John Wells
2006-Feb-08 21:59 UTC
[Rails] Idiomatic way to change partial''s behavior based on caller
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
--
Posted with http://DevLists.com. Sign up and save your time!
San
2006-Feb-08 22:44 UTC
[Rails] Re: Idiomatic way to change partial''s behavior based on caller
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 Wells wrote:> 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
>
>
>
>
John Wells
2006-Feb-08 23:27 UTC
[Rails] Re: Idiomatic way to change partial''s behavior based on
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 -- Posted with http://DevLists.com. Sign up and save your time!