Forum: Ruby on Rails Idiomatic way to change partial's behavior based on caller

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
John W. (Guest)
on 2006-02-09 00:00
(Received via mailing list)
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.rhtml -->
<tr>
  <td><%=part.number%></td>
  <td><%=part.associated_part_number%></td>
  <td><%=part.drawings%></td>
  <td><%=link_to "Choose this part", {:action=>"choose_part",
:id=>part.id}, :confirm=>"Are you sure?"%></td>
</tr>

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 <td/> 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
San (Guest)
on 2006-02-09 00:44
(Received via mailing list)
Hi John,

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

then

<td><%= inner_td %></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
John W. (Guest)
on 2006-02-09 01:27
(Received via mailing list)
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
This topic is locked and can not be replied to.