# render :update problem

**URL:** <https://discuss.rubyonrails.org/t/render-update-problem/39750>\
**Category:** rubyonrails-talk\
**Created:** [February 24, 2009, 6:14pm UTC](https://discuss.rubyonrails.org/t/render-update-problem/39750 "2009-02-24T18:14:49Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chris\_Habgood](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@Chris\_Habgood](https://discuss.rubyonrails.org/u/Chris_Habgood)\
**Post date:** [February 24, 2009, 6:14pm UTC](https://discuss.rubyonrails.org/t/render-update-problem/39750/1 "2009-02-24T18:14:49Z")

</div>

I have this div in my application.rhtml file:

\<div id="shelfcards"\> \<%= render :partial =\> 'ericssoncards' %\>\</div\>

When I go to another view I am doing this:

def turin2000 &nbsp;&nbsp;&nbsp;&nbsp;render :update do |page| &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;page[:shelfcards].replace\_html :partial =\> 'turin2000' &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;end

I get this for an output:

try { $("shelfcards").update("\<fieldset style='width: 400px;'\> \n\<legend\>Select your card type\</legend\>\n\n\t\<div id='oc192' class='card' \>OC-192\</div\>\n\t\<div id='oc48' \t\tclass='card' \>OC-48\</

> \n\t\<div id='oc12' \t\tclass='card' \>OC-12\</div\>\n\t\<div id='oc3'

\t\tclass='card' \>OC-3\</div\>\<br /\>\<br /\>\n\t\<div id='28pds1' \tclass='card' \>28P DS-1\</div\>\<br /\>\<br /\>\n\t\<div id='egcm' \t \tclass='card' \>EGCM\</div\>\<br /\>\<br /\>\n\t\<div id='gbeth' \tclass='card' \>GB ETH\</div\>\<br /\>\<br /\>\n\t\<div id='vt5g' \t \tclass='card' \>VT 5G\</div\>\<br /\>\<br /\>\n\</fieldset\> \t\t"); } catch (e) { alert('RJS error:\n\n' + e.toString()); alert('$ (\"shelfcards\").update(\"\<fieldset style=\'width: 400px;\'\>\ \n\<legend\>Select your card type\</legend\>\\n\\n\\t\<div id=\'oc192\' class=\'card\' \>OC-192\</div\>\\n\\t\<div id=\'oc48\' \\t\\tclass=\'card \' \>OC-48\</div\>\\n\\t\<div id=\'oc12\' \\t\\tclass=\'card\' \>OC-12\</div\> \\n\\t\<div id=\'oc3\' \\t\\tclass=\'card\' \>OC-3\</div\>\<br /\>\<br /\>\\n\ \t\<div id=\'28pds1\' \\tclass=\'card\' \>28P DS-1\</div\>\<br /\>\<br /\>\\n\ \t\<div id=\'egcm\' \\t\\tclass=\'card\' \>EGCM\</div\>\<br /\>\<br /\>\\n\ \t\<div id=\'gbeth\' \\tclass=\'card\' \>GB ETH\</div\>\<br /\>\<br /\>\\n\ \t\<div id=\'vt5g\' \\t\\tclass=\'card\' \>VT 5G\</div\>\<br /\>\<br /\>\\n\</

> \\t\\t\");'); throw e }

If I put this partial inside the div manually it works fine.

What causes this?

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [February 24, 2009, 6:31pm UTC](https://discuss.rubyonrails.org/t/render-update-problem/39750/2 "2009-02-24T18:31:14Z")

</div>

> I have this div in my application.rhtml file:
> 
> \<div id="shelfcards"\> \<%= render :partial =\> 'ericssoncards' %\>\</div\>
> 
> When I go to another view I am doing this:
> 
> def turin2000 &nbsp;&nbsp;&nbsp;&nbsp;render :update do |page| &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;page[:shelfcards].replace\_html :partial =\> 'turin2000' &nbsp;&nbsp;&nbsp;&nbsp;end end

What does the think making the ajax request look like ?

Fred

---

<div class="post-metadata">

**Author:** ![Chris\_Habgood](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@Chris\_Habgood](https://discuss.rubyonrails.org/u/Chris_Habgood)\
**Post date:** [February 24, 2009, 6:32pm UTC](https://discuss.rubyonrails.org/t/render-update-problem/39750/3 "2009-02-24T18:32:40Z")

</div>

Not an ajax call. Does it only work for an ajax call?

---

<div class="post-metadata">

**Author:** ![Chris\_Habgood](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@Chris\_Habgood](https://discuss.rubyonrails.org/u/Chris_Habgood)\
**Post date:** [February 24, 2009, 8:02pm UTC](https://discuss.rubyonrails.org/t/render-update-problem/39750/4 "2009-02-24T20:02:18Z")

</div>

All I am trying to do is to put a different partial in place of the the once that is already there. Not an ajax call to the function.

---

<div class="post-metadata">

**Author:** ![Eric9](https://avatars.discourse-cdn.com/v4/letter/e/ac91a4/32.png) [@Eric9](https://discuss.rubyonrails.org/u/Eric9)\
**Post date:** [February 24, 2009, 8:54pm UTC](https://discuss.rubyonrails.org/t/render-update-problem/39750/5 "2009-02-24T20:54:38Z")

</div>

Is there an error in the log at all for your current implementation?

---

<div class="post-metadata">

**Author:** ![Chris\_Habgood](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@Chris\_Habgood](https://discuss.rubyonrails.org/u/Chris_Habgood)\
**Post date:** [February 24, 2009, 9:00pm UTC](https://discuss.rubyonrails.org/t/render-update-problem/39750/6 "2009-02-24T21:00:14Z")

</div>

Well the previous TRY error filled the screen.

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [February 24, 2009, 10:32pm UTC](https://discuss.rubyonrails.org/t/render-update-problem/39750/7 "2009-02-24T22:32:01Z")

</div>

> Not an ajax call. Does it only work for an ajax call?

Yes. if you use render :update it returns some javascript. The  
prototype (and jquery etc.) libraries know to execute that javascript,  
but just a generic browser page load doesn't

If you want to replace a page fragment you have to use an ajax request  
(although you don't have to use render :update):

if you have link\_to\_remote 'Click me', :update =\> 'some\_id', :url =\>  
{:action =\> 'foo'}

and then

def foo &nbsp;&nbsp;&nbsp;render ... end

Then the results of that render will get stuck in the page element  
with id some\_id. You only need the render :update / rjs mechanism to  
update multiple things on the page.

Fred

---

<div class="post-metadata">

**Author:** ![Chris\_Habgood](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@Chris\_Habgood](https://discuss.rubyonrails.org/u/Chris_Habgood)\
**Post date:** [February 24, 2009, 10:40pm UTC](https://discuss.rubyonrails.org/t/render-update-problem/39750/8 "2009-02-24T22:40:23Z")

</div>

Well the “page fragment” is a div with a partial in it. I need to be able to make a regular call and update the div with the partial. I have a select box used to go to a page. I need to be able to change out what is in the div.

\<%= select(:node, :id, $shelves,{:prompt =\>‘Select Equipment’},{ :onchange =\> "document.location.href = ‘/buildshelf/’ + this.value "} ) %\>

This div:

 

has the partial for the cards in the shelf.

---

<div class="post-metadata">

**Author:** ![Chris\_Habgood](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@Chris\_Habgood](https://discuss.rubyonrails.org/u/Chris_Habgood)\
**Post date:** [February 24, 2009, 10:49pm UTC](https://discuss.rubyonrails.org/t/render-update-problem/39750/9 "2009-02-24T22:49:42Z")

</div>

I am baffled because in the view I do this:

$('shelfcards').update(\<%= render :partial =\> 'turin2000' %\>);

Gives me nothing in the div.

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [February 25, 2009, 8:09am UTC](https://discuss.rubyonrails.org/t/render-update-problem/39750/10 "2009-02-25T08:09:52Z")

</div>

Think about what actually ends up in the page:

$('shelfcards').update(\<fieldset\>....\</fieldset\>) Clearly that isn't legal javascript because you've just dumped in a chunk of html, whereas you need a string containing html

$('shelfcards').update('\<%= render :partial =\> 'turin2000' %\>');

would be a start, but would also be invalid if what you rendered contained a '

Fred

---

<div class="post-metadata">

**Author:** ![Jeremy\_Olliver](https://avatars.discourse-cdn.com/v4/letter/j/22d042/32.png) [@Jeremy\_Olliver](https://discuss.rubyonrails.org/u/Jeremy_Olliver)\
**Post date:** [February 25, 2009, 12:21pm UTC](https://discuss.rubyonrails.org/t/render-update-problem/39750/11 "2009-02-25T12:21:20Z")

</div>

Do note, that this is not really ajax. The point of ajax, and more importantly the use of rendering a partial is that when you make the request you are not reloading the entire page, but rather using javascript to send a request to the server in the background while the existing page doesn't reload. Using the javascript document.location.href call makes a full page load (regular html page request). To implement this instead with ajax you might instead have the select element inside a form, which you would post to a controller action (you can send the form using the onchange option for the select tag).

The reason for this is because the partial contains only part of a valid html page as Fred mentioned, using this method the controller action responding to the call, can then respond with something like page.replace\_html :partial =\> "my\_partial", which is some javascript telling the page that send the request to replace part of it's old html with the new version (sent as a string inside the javascript). Doing it this way will get teh ajax request working correctly (a true ajax call, with an rjs or render(:update) controller response). It's either that or return an entire html page with fresh contents on the page called by '/buildshelf/' + this.value "

Cheers, Jeremy

---

<div class="post-metadata">

**Author:** ![Chris\_Habgood](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@Chris\_Habgood](https://discuss.rubyonrails.org/u/Chris_Habgood)\
**Post date:** [February 25, 2009, 3:02pm UTC](https://discuss.rubyonrails.org/t/render-update-problem/39750/12 "2009-02-25T15:02:08Z")

</div>

Ya, I am not doing an ajax call because it is not really needed. I can possibly have new equipment and that would require more maintenance to tell the ajax call where to go for that piece of eqpt. The way I am doing would just require an array entry and a controller action. Anyway, I got it to work by doing this in the view:

$(‘shelfcards’).innerHTML = (\<%= render :partial =\> ‘turin2000’ %\>);
