Main menu generated from database


I'd like to have the main menu of my web application dynamically
generated from categories stored in a database.

I created a partial for this menu:

<% @categories.each do |category| %>
  <li><%= %></li>
<% end %>

And I call this partial in the main layout:

<%= render 'category/list' %>

Unfortunately, the @categories variable is Nil because the category/list
controller is not called with this method.

How can I simply achieve this ? Thank you,


In your application_controller put a before_filter that sets up
@categories. This will then be setup for all controllers and actions.
You might want to use a different name such as @categories_for_menu
or something so that it does not clash with other uses of the variable


I'd also recommend you cache the results so every page is not making a
DB call every time.


Thank you for your answer and your advice, that's what I was looking