I''m trying to write a little function to help render_partial a bit of
my page,
and I want to be able to call it from both the Controller and the View. Just
to make it concrete:
def render_favbutton(event)
render(:partial=>''shared/favorite'',
:locals => { :imgstar =>
"/images/button_sing_star_hov.png",
:imgnostar =>
"/images/button_sing_star.png" },
:object => event)
end
But I can''t make this work. If I put this in a helper module, then
it''s only
accessible from the View and not the Controller. If I put this in
ApplicationController, then it always calls the ActionController render
function, so when the View invokes my helper function, I get a Double Render
error.
What is the right way to make this work? The best I came up with is to
explicitly pass in self.method[:render] so that it calls the right render.
Thanks!
Daniel Peng
Ezra Zygmuntowicz
2005-Oct-14 15:25 UTC
Re: Sharing helper function between Controller and View
On Oct 13, 2005, at 11:58 PM, Daniel J Peng wrote:> I''m trying to write a little function to help render_partial a bit > of my page, > and I want to be able to call it from both the Controller and the > View. Just > to make it concrete: > > def render_favbutton(event) > render(:partial=>''shared/favorite'', > :locals => { :imgstar => "/images/ > button_sing_star_hov.png", > :imgnostar => "/images/ > button_sing_star.png" }, > :object => event) > end > > But I can''t make this work. If I put this in a helper module, then > it''s only > accessible from the View and not the Controller. If I put this in > ApplicationController, then it always calls the ActionController > render > function, so when the View invokes my helper function, I get a > Double Render > error. > > What is the right way to make this work? The best I came up with > is to > explicitly pass in self.method[:render] so that it calls the right > render. > > Thanks! > Daniel Peng > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > >Daniel- Here''s a snippet from the api.rubyonrails.com: helper_method(*methods) Declare a controller method as a helper. For example, helper_method :link_to def link_to(name, options) ... end makes the link_to controller method available in the view. Cheers- -Ezra Zygmuntowicz WebMaster Yakima Herald-Republic Newspaper ezra-gdxLOakOTQ9oetBuM9ipNAC/G2K4zDHf@public.gmane.org 509-577-7732 _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Daniel J Peng
2005-Oct-14 15:46 UTC
Re: Sharing helper function between Controller and View
Ezra, Thank you for your help. However, I don''t think that it solves the problem, because my function needs to call ''render'' in different classes, depending on whether it is invoked from the View or the Controller. Is there any way to do that elegantly? Daniel On Fri, Oct 14, 2005 at 08:25:59AM -0700, Ezra Zygmuntowicz wrote:> On Oct 13, 2005, at 11:58 PM, Daniel J Peng wrote: > > I''m trying to write a little function to help render_partial a bit of my > page, > and I want to be able to call it from both the Controller and the View. > Just > to make it concrete: > def render_favbutton(event) > render(:partial=>''shared/favorite'', > :locals => { :imgstar => "/images/button_sing_star_hov.png", > :imgnostar => "/images/button_sing_star.png" }, > :object => event) > end > But I can''t make this work. If I put this in a helper module, then it''s > only > accessible from the View and not the Controller. If I put this in > ApplicationController, then it always calls the ActionController render > function, so when the View invokes my helper function, I get a Double > Render > error. > What is the right way to make this work? The best I came up with is to > explicitly pass in self.method[:render] so that it calls the right > render. > Thanks! > Daniel Peng > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > Daniel- > Here''s a snippet from the api.rubyonrails.com: > helper_method(*methods) > > Declare a controller method as a helper. For example, > > helper_method :link_to > def link_to(name, options) ... end > > makes the link_to controller method available in the view. > > Cheers- > -Ezra Zygmuntowicz > WebMaster > Yakima Herald-Republic Newspaper > ezra-gdxLOakOTQ9oetBuM9ipNAC/G2K4zDHf@public.gmane.org > 509-577-7732> _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails
Ezra Zygmuntowicz
2005-Oct-14 17:30 UTC
Re: Sharing helper function between Controller and View
Daniel-
I wonder if you could use render_to_string? I''m not sure if you
use that and then render a view and pass the rendered string to it if
it will fail with the double render error or not. Something to try
though.
Can you maybe explain what exactly you are trying to do a little
more? If it needs different behavior when called from the view than
when called from the controller, maybe you could add anothe arg to
the method and pass in a flag that says whether you called it from
the view or the controller. Then use a case statement or some
if..else//end blocks to do the correct render. Kind of a kludge but
I''m not coming up with a better thought at the moment.
HTH-
-Ezra
On Oct 14, 2005, at 8:46 AM, Daniel J Peng wrote:
> Ezra,
>
> Thank you for your help. However, I don''t think that it solves
the
> problem, because my
> function needs to call ''render'' in different classes,
depending on
> whether it is invoked
> from the View or the Controller. Is there any way to do that
> elegantly?
>
> Daniel
>
> On Fri, Oct 14, 2005 at 08:25:59AM -0700, Ezra Zygmuntowicz wrote:
>
>> On Oct 13, 2005, at 11:58 PM, Daniel J Peng wrote:
>>
>> I''m trying to write a little function to help
render_partial
>> a bit of my
>> page,
>> and I want to be able to call it from both the Controller and
>> the View.
>> Just
>> to make it concrete:
>> def render_favbutton(event)
>> render(:partial=>''shared/favorite'',
>> :locals => { :imgstar => "/images/
>> button_sing_star_hov.png",
>> :imgnostar => "/images/
>> button_sing_star.png" },
>> :object => event)
>> end
>> But I can''t make this work. If I put this in a helper
>> module, then it''s
>> only
>> accessible from the View and not the Controller. If I put
>> this in
>> ApplicationController, then it always calls the
>> ActionController render
>> function, so when the View invokes my helper function, I get
>> a Double
>> Render
>> error.
>> What is the right way to make this work? The best I came up
>> with is to
>> explicitly pass in self.method[:render] so that it calls the
>> right
>> render.
>> Thanks!
>> Daniel Peng
>> _______________________________________________
>> Rails mailing list
>> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
>> http://lists.rubyonrails.org/mailman/listinfo/rails
>>
>> Daniel-
>> Here''s a snippet from the api.rubyonrails.com:
>> helper_method(*methods)
>>
>> Declare a controller method as a helper. For example,
>>
>> helper_method :link_to
>> def link_to(name, options) ... end
>>
>> makes the link_to controller method available in the view.
>>
>> Cheers-
>> -Ezra Zygmuntowicz
>> WebMaster
>> Yakima Herald-Republic Newspaper
>> ezra-gdxLOakOTQ9oetBuM9ipNAC/G2K4zDHf@public.gmane.org
>> 509-577-7732
>>
>
>
>> _______________________________________________
>> Rails mailing list
>> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
>> http://lists.rubyonrails.org/mailman/listinfo/rails
>>
>
> _______________________________________________
> Rails mailing list
> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>
>
-Ezra Zygmuntowicz
Yakima Herald-Republic
WebMaster
http://yakimaherald.com
509-577-7732
ezra-gdxLOakOTQ9oetBuM9ipNAC/G2K4zDHf@public.gmane.org
Matthew Denner
2005-Oct-14 18:23 UTC
Re: Sharing helper function between Controller and View
On 10/14/05, Daniel J Peng <spam+rails-FjP/GabXaWhN0TnZuCh8vA@public.gmane.org> wrote:> Thank you for your help. However, I don''t think that it solves the problem, because my > function needs to call ''render'' in different classes, depending on whether it is invoked > from the View or the Controller. Is there any way to do that elegantly?Have you tried defining the method in a helper, then in the controller doing something like: class MyController < ApplicationController helper ''My'' include MyHelper end Can''t remember if that''s possible or not but might be worth a try. Matt