Hello, 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: (views/category/_list.html.erb) <ul> <% @categories.each do |category| %> <li><%= category.name %></li> <% end %> </ul> And I call this partial in the main layout: (layouts/application.html.erb): <%= 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, Fabrice -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 1 March 2011 08:44, Fabrice Fabrisss <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Hello, > > 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: > (views/category/_list.html.erb) > > <ul> > <% @categories.each do |category| %> > <li><%= category.name %></li> > <% end %> > </ul> > > > And I call this partial in the main layout: > (layouts/application.html.erb): > > <%= 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 @categories. Colin -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
I''d also recommend you cache the results so every page is not making a DB call every time. Brian -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Thank you for your answer and your advice, that''s what I was looking for, Sincerely, Fabrice -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.