RoR侧面内容显示在多个页面上

时间:2013-02-05 14:27:53

标签: ruby-on-rails partial-views

如果您的页面上有一个包含数据库信息的边桌,并且您希望它显示在您的RoR应用程序的多个页面上。该怎么做? 假设您在视图中输出此数据,称为LeftTableView或类似的东西。

现在,在“/ products /”视图中,您要显示此数据,并且还要在“/ products / 12”和“/ friends /”等上显示它​​。

如何渲染这个“部分”视图并仅使用原始控制器绑定数据库数据?或者......从每个控制器再次收集数据“更好”吗?

2 个答案:

答案 0 :(得分:1)

假设,如果要在数据库导航中显示“类别”,请保留在application_controller.rb

def navcategory
    @navcategories = Category.all
end

在products_controller.rb和friends_controller.rb上,您只需使用before_filter调用navcategory

before_filter :navcategory

和布局

<% @navcategories.each do |category| %> 
....
....
....
<% end %>

答案 1 :(得分:0)

你应该设置你的布局。尝试以下(没有css)

# application layout
<body>
  <div id='sidebar'>
    ... common content here
  </div>
  <div id='main'>
    <%= yield %>
  </div>
</body>
相关问题