# Rendered as needed

**URL:** <https://www.ruby-forum.com/t/rendered-as-needed/173931>\
**Category:** Rails\
**Created:** [August 18, 2009, 12:47pm UTC](https://www.ruby-forum.com/t/rendered-as-needed/173931 "2009-08-18T12:47:58Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![RVince](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/4491bb/32.png) [@RVince](https://www.ruby-forum.com/u/RVince)\
**Post date:** [August 18, 2009, 12:47pm UTC](https://www.ruby-forum.com/t/rendered-as-needed/173931/1 "2009-08-18T12:47:58Z")

</div>

Is there a way to get a link to a partial to open in a div, which I  
can open with  
the prototype .toggle() ?

What I want is to put a link on a page, which, when clicked, renders  
the partial in a div in that page. However, I dont want the partial  
merely sitting, already rendered in a hidden div, but rather, to be  
called and rendered when the link is hit (i.e. minimal transmission to  
the client) Is there a way I can do this? Thanks, RVince

---

<div class="post-metadata">

**Author:** ![RVince](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/4491bb/32.png) [@RVince](https://www.ruby-forum.com/u/RVince)\
**Post date:** [August 18, 2009, 5:14pm UTC](https://www.ruby-forum.com/t/rendered-as-needed/173931/2 "2009-08-18T17:14:57Z")

</div>

You can’t do both of these things at the same time.  
If you want to use toggle, the content should already be there and may  
be hidden.

If you want to generate the content when the link is clicked, then you  
should use link\_to\_remote to render the link,  
Then you can render a partial in your rjs template have something like  
this:  
page.replace\_html, :partial =\> ‘example’

---

<div class="post-metadata">

**Author:** ![RVince](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/4491bb/32.png) [@RVince](https://www.ruby-forum.com/u/RVince)\
**Post date:** [August 18, 2009, 5:22pm UTC](https://www.ruby-forum.com/t/rendered-as-needed/173931/3 "2009-08-18T17:22:34Z")

</div>

So, say, in page a I have data in rows, say. On each row, a link. That  
link should open a form between the rows. YOu are saying there is a  
way I can do that with link\_to\_remote somehow

---

<div class="post-metadata">

**Author:** ![RVince](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/4491bb/32.png) [@RVince](https://www.ruby-forum.com/u/RVince)\
**Post date:** [August 18, 2009, 9:16pm UTC](https://www.ruby-forum.com/t/rendered-as-needed/173931/4 "2009-08-18T21:16:48Z")

</div>

Thank you so much – this is exactly the solution I need. -RVince

---

<div class="post-metadata">

**Author:** ![RVince](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/4491bb/32.png) [@RVince](https://www.ruby-forum.com/u/RVince)\
**Post date:** [August 18, 2009, 8:49pm UTC](https://www.ruby-forum.com/t/rendered-as-needed/173931/5 "2009-08-18T20:49:52Z")

</div>

Yes, link\_to\_remote will call some action in your controller. That  
action can generate javascript to update portion of your page.  
That controller can have an some-action.rjs template (instead of the  
usual some-action.html.erb)  
That template could look something like this:

page.replace\_html ‘id-of-some-element-on-your-page’, :partial =\>  
‘partial you want to render there’

You could also render it directly in your controller if you prefer.

Check out the following documentation:  
[http://api.rubyonrails.org/classes/ActionView/Helpers/PrototypeHelper/JavaScriptGenerator/GeneratorMethods.html#M001666](http://api.rubyonrails.org/classes/ActionView/Helpers/PrototypeHelper/JavaScriptGenerator/GeneratorMethods.html#M001666)  
There are plenty of examples there.

I might also recommend the peepcode screencast covering rjs. It walks  
through several simple examples.
