Multiple 'render :partial' in a helper?


#1

Hi all,

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

problem: the 1st ‘render :partial’ has no effect

Alain


#2

Alain,

On 2.2.2006, at 13.07, Alain R. wrote:

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.

//jarkko


#3

Jarkko

Of course, you’re right. What was I thinking?

   > 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