简体   繁体   English

轨道如何渲染来自另一个控制器的页面

[英]rails how to render a page which is from another controller

Hi I am trying to render a view from one page to another pager that 嗨,我正在尝试将一个页面的视图呈现到另一个页面

I want render companyratings/show in pages/companies_list. 我要在页面/ companies_list中显示公司评级/显示。 So I have used the following code to do that 所以我用下面的代码来做到这一点

in companies_list.html.erb 在companies_list.html.erb中

<%= render :partial => 'companyratings/show' %>

but it showing an error in my companyrating/show page NoMethodError in Pages#companies_list 但是它在Pages#companies_list的我的公司评分/显示页面NoMethodError中显示错误

at undefined method `each' for nil:NilClass

this my content in my companyratings/show 这是我在companyratings / show中的内容

<title>Shared Todo App </title>
<h1>Shared Todo App</h1>
<p>All your todos here</p>
<table border="3">                         
  <tr>                          
    <th>Name</th>
    <th>place</th>
    <th>Rating</th>
    <th>Rank</th>
  </tr>
<% @companies.each do |companies| %>     
  <tr>
    <td>  <%= companies.name%> </td>  
    <td><%= companies.place%></td> 
    <td><%= companies.rate%>%</td> 
    <td><%= companies.rank%></td>  
  </tr>
<% end %>                        
</table> 

this is my companies_list file 这是我的companies_list文件

<% if signed_in? %>
  <div class="row">
    <aside class="span4">
     <section>
                   <%= render 'shared/user_info' %>
 <%= render :partial => 'companyratings/show' %>

      </section>

  </div>  
<% else %>
<%end%>

Here is the controller for companyratings 这是公司评级的控制器

class CompanyratingsController < ApplicationController
    before_action :signed_in_user, only: [:create, :destroy]
    before_action :correct_user,   only: :destroy


def index
     @companies = Companyrating.all
     @companyrating = Companyrating.new

  end

def show
    @companyrating = Companyrating.new
    @companies = Companyrating.all
end



def create
  @companies = Companyrating.new(params[:companyrating])
  if @companies.save
    flash[:success] = "Welcome to My Space!"
    redirect_to :action => 'index'
  else
    flash[:error] = "Can't create companyrating."
    render 'index'
  end
end


 private
 def new_company_rating
      params.require(:company_rating).permit(:name, :place, :rate, :rank, :user_id)
 end
def correct_user
      @companies = current_user.companies.find_by(id: params[:id])
      redirect_to root_url if @companyrating.nil?
    end


end

this is my controller code for pages controller 这是我的页面控制器代码

class PagesController < ApplicationController
  def companies_list
    @title = "companies_list"
    if signed_in?
       @companies_mines  = current_user.companies_mines.build



    end
  end


end

So can any one help how to do this 那么任何人都可以帮忙怎么做

It's caused by the fact, that you're probably not setting @companies variable in method responsible for rendering companies_list file. 这是由于您可能未在负责呈现companies_list文件的方法中设置@companies变量@companies

We can't tell you anything more until you provide us controller code. 在您提供控制器代码之前,我们无法告诉您更多信息。

You will need to have @companies on hand and pass it along with locals . 您需要手头有@companies并将其与locals一起传递。 Then reference the local variable. 然后引用局部变量。

http://guides.rubyonrails.org/layouts_and_rendering.html#using-partials http://guides.rubyonrails.org/layouts_and_rendering.html#using-partials

Look at 3.4.6 local variables 看3.4.6局部变量

your doing right way but for solved to that error you have to do some following changes. 您的做法是正确的,但要解决该错误,您必须进行以下一些更改。

as you have the def companies_list 有了def companies_list

in your page controller, here there is no instance for @companies as you have used in your partial('companyratings/show'). 在页面控制器中,这里没有@companies实例,就像您在partial('companyratings / show')中使用的一样。

The error occurred because you are not sending any @companies as locales and you have used looping thought it. 发生此错误是因为您没有将任何@companies作为区域设置发送,并且您已经使用循环了。

so add @companies instance in def companies_list and send as locales into partial like this 因此,在def companies_list中添加@companies实例,并将其作为语言环境发送到类似的partial中

<%= render :partial => 'companyratings/show', locals=>{:companies => @companies } %>

and use companies instance instead of @companies. 并使用公司实例代替@companies。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM