Constantin Gavrilescu
2009-Jun-14 04:31 UTC
in a view, call a helper method from another controller
I need to call a helper method from another controller. The catch is that I have the same method name defined in my current controllers helper. Here the partial will use the the default column_list defined in my helper. <%=render ''companies/company'', :object => @company%> For example, if I change the call to specify the method, it still runs great: <%=render ''companies/company'', :object => @company, :locals => {:column_list => method(:column_list)}%> Here I want to use another''s helper column_list: <%=render ''companies/companies'', :object => @customers_employees, :locals => {:column_list => CustomersEmployeesController.new.method(:column_list)} %> I get this error: undefined method `column_list'' for class `CustomersEmployeesController'' How can I pass the reference to the right helper method?
Frederick Cheung
2009-Jun-14 10:18 UTC
Re: in a view, call a helper method from another controller
On Jun 14, 5:31 am, Constantin Gavrilescu <comisarulmoldo...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Here I want to use another''s helper column_list: > <%=render ''companies/companies'', :object => > @customers_employees, :locals => {:column_list => > CustomersEmployeesController.new.method(:column_list)} %> > > I get this error: > undefined method `column_list'' for class > `CustomersEmployeesController'' > > How can I pass the reference to the right helper method?helper_methods are not methods of the controller - they are methods of the associated helper module (usually CustomerEmployeesHelper in this case). Does that help ? Fred
Constantin Gavrilescu
2009-Jun-19 21:28 UTC
Re: in a view, call a helper method from another controller
On 14 juin, 05:18, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Jun 14, 5:31 am, Constantin Gavrilescu > > <comisarulmoldo...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Here I want to use another''s helper column_list: > > <%=render ''companies/companies'', :object => > > @customers_employees, :locals => {:column_list => > > CustomersEmployeesController.new.method(:column_list)} %> > > > I get this error: > > undefined method `column_list'' for class > > `CustomersEmployeesController'' > > > How can I pass the reference to the right helper method? > > helper_methods are not methods of the controller - they are methods of > the associated helper module (usually CustomerEmployeesHelper in this > case). Does that help ? >I realize that. I want to call a helper method column_list available when running CustomersEmployeesController. CustomersEmployeesController inherits from CompaniesController, which includes the helper method needed. I cannot hardcode the file name of the module where this method is, because I might redefine it later in CustomersEmployeesHelper and that would drive crazy the maintenance programmer when will find that normal mechanisms of inheritance don''t work. I thought that instantiating CustomersEmployeesController will include automatically his Helper module, but it appears it doesn''t.
Frederick Cheung
2009-Jun-19 21:57 UTC
Re: in a view, call a helper method from another controller
On Jun 19, 10:28 pm, Constantin Gavrilescu <comisarulmoldo...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 14 juin, 05:18, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: > > > > How can I pass the reference to the right helper method? > > > helper_methods are not methods of the controller - they are methods of > > the associated helper module (usually CustomerEmployeesHelper in this > > case). Does that help ? > > I realize that. > I want to call a helper method column_list available when running > CustomersEmployeesController. CustomersEmployeesController inherits > from CompaniesController, which includes the helper method needed. I > cannot hardcode the file name of the module where this method is, > because I might redefine it later in CustomersEmployeesHelper and that > would drive crazy the maintenance programmer when will find that > normal mechanisms of inheritance don''t work. > > I thought that instantiating CustomersEmployeesController will include > automatically his Helper module, but it appears it doesn''t.Why not put the relevant helper methods in a module that both controllers use ? Fred