On Oct 21, 2005, at 4:55 AM, Peter Donald wrote:
> Hi,
>
> I have just upgraded to the latest version of rails and zero arg
> initializers in helpers stopped working. ie I previously had
> something like
>
> module CheeseHelper
> def initialize
> ... init some vars etc ...
> end
> end
>
> class GobbleController < ApplicationController
> helper :cheese
> end
Hmm. By putting an initialize method in your helper, you were
overriding the initialize method on the ActionView::Base class when
the helper was included, which is not a good thing (especially since
the initialize method is private and cannot be guaranteed to remain
unchanged between releases).
Helpers ought to be stateless, but if you really need some persistent
state available to a helper you can set them in instance variables on
the controller, initialized via a before_filter or something.
Instance variables in the controller are always made available to the
view.
- Jamis
>
> But this will generate an exception down in the guts of the
> framework because something has a 3-arg initialize or similar...
>
> ie.
> wrong number of arguments (3 for 0) c:/applications/ruby/lib/ruby/
> gems/1.8/gems/actionpack-1.10.1/lib/action_controller/base.rb:
> 804:in `initialize''
> c:/applications/ruby/lib/ruby/gems/1.8/gems/actionpack-1.10.1/lib/
> action_controller/base.rb:804:in `new''
> c:/applications/ruby/lib/ruby/gems/1.8/gems/actionpack-1.10.1/lib/
> action_controller/base.rb:804:in `initialize_template_class''
> c:/applications/ruby/lib/ruby/gems/1.8/gems/actionpack-1.10.1/lib/
> action_controller/base.rb:357:in
> `process_without_session_management_support''
> c:/applications/ruby/lib/ruby/gems/1.8/gems/actionpack-1.10.1/lib/
> action_controller/session_management.rb:116:in `process''
> c:/applications/ruby/lib/ruby/gems/1.8/gems/rails-0.14.1/lib/
> dispatcher.rb:36:in `dispatch''
> c:/applications/ruby/lib/ruby/gems/1.8/gems/rails-0.14.1/lib/
> webrick_server.rb:117:in `handle_dispatch''
> c:/applications/ruby/lib/ruby/gems/1.8/gems/rails-0.14.1/lib/
> webrick_server.rb:83:in `service''
> ...
>
> So how do you do initialization in the helpers?
>
> Cheers,
>
> Peter Donald
>
> _______________________________________________
> Rails mailing list
> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>