I’d like to move some view code to a helper but it doesn’t work because
I need to render two partials each time.
Q: Is it possible to do this with a helper?
Example:
Before: In the view:
…
<% case controller.controller_name
when ‘member’ %>
<%= render :partial => ‘shared/member_menu’ %>
<%= render :partial => ‘shared/member_submenu’ %>
…
<% end %>
…
After: In the view:
<%= generate_menu %>
After: in application_helper.rb (DOESN’T WORK)
def generate_menu
case controller.controller_name
when ‘member’
render :partial => ‘shared/member_menu’ <— HAS NO
EFFECT !!
render :partial => ‘shared/member_submenu’
…
end
end
EFFECT !!
render :partial => ‘shared/member_submenu’
…
end
end
problem: the 1st ‘render :partial’ has no effect
Remember that helpers are methods that return something, so they
can’t render directly to the response like you seem to assume above.
By default they will return the last evaluated line, in this case
what your second render line returns.
You can probably concatenate the two to a single return value.
> You can probably concatenate the two to a single return value.
Yes :
def generate_menu
case controller.controller_name
when ‘member’
a = render :partial => ‘shared/members_avantages_menu’
b = render :partial => ‘shared/member_submenu’
a+b <<---- CONCATENATION
when …
…
end
end
Thanks again for waking me up.
Alain
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.