I was in a similar situation as well, and what Elliott has suggested
will work. However, you have to realize that the initialization of
that @tags variable will happen for every single call made to any
method belonging to any of the controllers. This is because the
controllers inherit from ApplicationController, and that filter is run
everytime before any controller method runs, so you''d effectively be
doing your initialization of stuff not only when your rails
application starts, but also each time a controller''s method is
invoked.
I''m not sure what the best way to avoid having this happen is, but
what I''ve done is created another class (say
''ClassFoo'') containing
the information I want to have initialized once, with this info being
contained in class variables, and accessed through class methods. In
the above init before_filter, I then make a call into some function
''foo'' which is a class method of ''ClassFoo'',
which performs all the
initialization. However since this class is not an actual controller,
it''s class variables can be made to be initialized only once (by
having some boolean class variable to check whether I''ve initialized
yet). So you''re still going to end up calling ''foo''
on each controller
method invocation, however the initialization can be controlled to
occur only once.
Hopefully that made some sense. I''m not claiming its the nicest way by
any means, but I couldn''t find any other way.
Cheers,
Amir
On Jan 28, 6:19 pm, Elliott Blatt
<rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>
wrote:> James,
>
> I believe that you want to put all youinitializationstuff in an
''init''
> method inside of application.rb. I don''t believe that helpers are
the
> best choice here.
>
> Try something like this:
>
> class ApplicationController < ActionController::Base
> before_filter :init
>
> def init
> @tags = Tag.tags(:limit => 200, :order => "name asc")
> end
>
> end
>
> Elliott Blattwww.elliottblatt.com
>
> --
> Posted viahttp://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-/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
-~----------~----~----~----~------~----~------~--~---