Ruby on rails rails layput中的局部视图
我想在布局页面中呈现一个部分,即application.html.erb和它的变量。 我在layouts文件夹“\u fellowshi\u sider.html.erb”中创建了部分视图,然后在application.html.erb中添加了下面的代码Ruby on rails rails layput中的局部视图,ruby-on-rails,partial-views,Ruby On Rails,Partial Views,我想在布局页面中呈现一个部分,即application.html.erb和它的变量。 我在layouts文件夹“\u fellowshi\u sider.html.erb”中创建了部分视图,然后在application.html.erb中添加了下面的代码 <%= render "layouts/fellowship_sider", :entries => @entries %> @entries%> 我无法理解在哪里设置@entries变量的值。我在applicatio
<%= render "layouts/fellowship_sider", :entries => @entries %>
@entries%>
我无法理解在哪里设置@entries变量的值。我在application\u controller中编写了函数,但它不起作用。如果布局中有此部分,则每次调用时可能都需要
@entries
对象,因此请检查application controller中的方法是否实际被调用,控制器中的before筛选器将为每个操作执行此操作
应用程序控制器:
class ApplicationController < ActionController::Base
before_filter :get_entries
def get_entries
@entries = Entry.all
end
end
<%= render "layouts/fellowship_sider" %>
<% @entries.each do |entry| %>
<%= entry.foo %>
<% end %>
\u fellowship\u sider.html.erb:
class ApplicationController < ActionController::Base
before_filter :get_entries
def get_entries
@entries = Entry.all
end
end
<%= render "layouts/fellowship_sider" %>
<% @entries.each do |entry| %>
<%= entry.foo %>
<% end %>
试试这个:
<%= render :partial => "layouts/fellowship_sider", :locals => {:entries => @entries } %>
“布局/伙伴关系”,:locals=>{:entries=>@entries}%>
谢谢。这里您将局部变量
条目传递给partial,它等于@entries
。所以在你的部分中,它应该没有“@”。@条目在团契中使用?如果是,则不需要将实例变量的作用域完全传递给视图。但是如果你仍然想传递它,它不应该是一个实例变量,而应该使用区域设置来传递它。或者你能提供更多的代码吗?仅仅一行渲染就很难判断是否需要传递entry变量@entries%>