So I have a layout (application.rhtml) that contains a list of categories that are stored within the database. To get retrieve the list I overwrite the initialize method within the application controller (application.rb) as such: def initialize @categories = Category.find(:all, :order => :position) end Is this the best way I should be doing this? I don''t want to have this code in my layout breaking MVC idioms, but is there a more preferred way in best practice then by doing it this way? Thanks, Blaine --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Personally I''d be writing a before filter to call a method in my application.rb Cam On Aug 16, 1:31 pm, blaine <jangc...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> So I have a layout (application.rhtml) that contains a list of > categories that are stored within the database. To get retrieve the > list I overwrite the initialize method within the application > controller (application.rb) as such: > > def initialize > @categories = Category.find(:all, :order => :position) > end > > Is this the best way I should be doing this? I don''t want to have > this code in my layout breaking MVC idioms, but is there a more > preferred way in best practice then by doing it this way? > > Thanks, > > Blaine--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Aug 16, 5:31 am, blaine <jangc...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> So I have a layout (application.rhtml) that contains a list of > categories that are stored within the database. To get retrieve the > list I overwrite the initialize method within the application > controller (application.rb) as such: > > def initialize > @categories = Category.find(:all, :order => :position) > end > > Is this the best way I should be doing this? I don''t want to have > this code in my layout breaking MVC idioms, but is there a more > preferred way in best practice then by doing it this way?If I understand you correctly, you should be using a before filter: http://api.rubyonrails.com/classes/ActionController/Filters/ClassMethods.html You may want to cache the list of categories until they change: http://ap.rubyonrails.com/classes/ActionController/Caching/Fragments.html --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Excellent - I thought about doing it with a before_filter, but again wasn''t sure. I''ve adjusted the code to use a before_filter instead and it''s all working great, thanks a lot. Blaine On Aug 16, 1:07 am, Henrik N <hen...-//VPbvzLDw4@public.gmane.org> wrote:> On Aug 16, 5:31 am, blaine <jangc...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > So I have a layout (application.rhtml) that contains a list of > > categories that are stored within the database. To get retrieve the > > list I overwrite the initialize method within the application > > controller (application.rb) as such: > > > def initialize > > @categories = Category.find(:all, :order => :position) > > end > > > Is this the best way I should be doing this? I don''t want to have > > this code in my layout breaking MVC idioms, but is there a more > > preferred way in best practice then by doing it this way? > > If I understand you correctly, you should be using a before filter:http://api.rubyonrails.com/classes/ActionController/Filters/ClassMeth... > > You may want to cache the list of categories until they change:http://ap.rubyonrails.com/classes/ActionController/Caching/Fragments....--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---