Idiomatic way to change partial's behavior based on caller


#1

Guys,

I have a partial that is shared by many controllers, and this partial
has a link that needs to do different things based on which controller
calls it.

So, the partial looks like this:

<%=part.number%> <%=part.associated_part_number%> <%=part.drawings%> <%=link_to "Choose this part", {:action=>"choose_part", :id=>part.id}, :confirm=>"Are you sure?"%>

What I’d like to do is change the link_to to be dynamic, and allow the
controller to set values to pass to the partial. I can see this working
in two ways, A: leave the link_to call there, but replace the paramters
with variables that the controller populate, or B: allow the controllers
to specify what appears in the fourth

entirely, which is in my
mind preferred.

What is the idiomatic way of doing this? Is A my only option, or can you
think of a way to do B?

Thanks in advance, as always!

John


#2

Hi John,

render :partial => ‘part’, :locals => { :inner_td =>
‘stuff_inside_the_td’ }

then

<%= inner_td %> in the template

or

render :partial => ‘part’, :locals => { :action => ‘choose’, :id => id,
:confirm => ‘really?’ }

and you can see how to fill in the link_to vars from above.

Both are fine solutions depending on your needs.

Cheers,

-San


#3

On Wednesday, February 08, 2006, at 2:43 PM, San wrote:

Hi John,

render :partial => ‘part’, :locals => { :inner_td =>
‘stuff_inside_the_td’ }

Hi San…thanks for the response.

What I’m curious about though, and I’m not at my code or I’d give it a
shot: how can you make :inner_td contain code that would be valid inside
the erb, such as link_to’s or form_tag’s, etc etc. Does this require a
call to eval within the erb?

Thanks for the help!

John