简体   繁体   English

Ruby on Rails全球类别阻止

[英]Ruby on Rails global categories block

I am new to ROR and I don't understand how can I make global template which I could put in any other template. 我是ROR的新手,我不明白如何制作可以放在其他模板中的全局模板。 For example I have categories module and would like to create sidebar navigation and put it at homepage template. 例如,我有类别模块,想要创建侧边栏导航并将其放在主页模板中。 I tried to do it this way, but categories controller method side_nav is never called. 我尝试通过这种方式进行操作,但从未调用类别控制器方法side_nav。 Is this good practice for this type of problem or should i do it different way? 这是针对此类问题的良好做法,还是我应该采用其他方式?

categories/categories_controller.rb 类别/categories_controller.rb

def side_nav
    @categories = Category.all
end

categories/_side_nav.html.erb 类别/_side_nav.html.erb

<ul class="list-unstyled">
  <% categories.each do |category| %>
    <li><%= link_to category.title, category.title.downcase %></li>
  <% end %> 
</ul>

homepage/index.html.erb homepage / index.html.erb

<%= render 'categories/side_nav' %>

You may look at layout/application file. 您可以查看布局/应用程序文件。 It's global layout in your custom project by default. 默认情况下,它是自定义项目中的全局布局。 And you can provide your custom layouts in your contollers. 而且,您可以在contollers中提供自定义布局。

http://guides.rubyonrails.org/layouts_and_rendering.html http://guides.rubyonrails.org/layouts_and_rendering.html

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

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