简体   繁体   中英

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. 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. Is this good practice for this type of problem or should i do it different way?

categories/categories_controller.rb

def side_nav
    @categories = Category.all
end

categories/_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

<%= 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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