# Render :update

**URL:** <https://www.ruby-forum.com/t/render-update/145986>\
**Category:** Rails\
**Created:** [August 15, 2008, 8:32pm UTC](https://www.ruby-forum.com/t/render-update/145986 "2008-08-15T20:32:47Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [August 15, 2008, 8:32pm UTC](https://www.ruby-forum.com/t/render-update/145986/1 "2008-08-15T20:32:47Z")

</div>

I have a list of names in a drop-down and when one of those names it is  
picked I want my form to update a div of those selected names but to  
also update the drop-down so that name no longer appears in that list,  
which means I need to update two divs. I have this working in the sense  
that the two seperate divs do get updated via a render :update do  
|page|…

##Controller code  
render :update do |page|  
page.replace\_html ‘saved\_travel\_arrangers\_div’, :partial =\>  
‘show\_my\_managers’  
page.replace\_html ‘travel\_managers\_list’, :partial =\>  
‘list\_managers\_dropdown’  
end

The problem is that ‘saved\_travel\_arrangers\_div’ gets filled with, well,  
to be honest, a bunch of shit! For example,

try { Element.update(“saved\_travel\_arrangers\_div”, "  
\n\t\n\t\t\n\t\t  
\n\t\t\t  
\n\t\t  
\n\t\t  
\n\t\t\t  
\n\t\t  
\n\t\t  
\n\t\t\t  
\n\t\t  
\n\t\t  
\n\t\t\t  
\n\t\t  
\n\t\t  
\n\t\t\t  
\n\t\t  
\n\t\t\n\t\n\t\t\n\t\t  
\n\t\t\t  
\n\t\t  
\n\t\t  
\n\t\t\t  
\n\t\t  
\n\t\t  
\n\t\t\t  
\n\t\t  
\n\t\t  
\n\t\t\t  
\n\t\t  
\n\t\t  
\n\t\t\t  
\n\t\t  
\n\t\t\n\t\n

Why is it putting this as output and how do I get rid of it? Thanks in  
advance for the help,

-S

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [August 15, 2008, 8:50pm UTC](https://www.ruby-forum.com/t/render-update/145986/2 "2008-08-15T20:50:19Z")

</div>

How are you including your javascript?

Lake

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [August 15, 2008, 10:03pm UTC](https://www.ruby-forum.com/t/render-update/145986/3 "2008-08-15T22:03:19Z")

</div>

Lake D. wrote:

> How are you including your javascript?
> 
> Lake

The action get intiated by the onchange of a dropdown, so I have:

\<%= select( “managersplususers”, “travel\_arranger\_id”,  
User.find( :all, :conditions =\> ['role = ? and primary\_account\_id = ?  
and id  
NOT IN( SELECT travel\_arranger\_id FROM Managersplususers WHERE user\_id  
= ‘’

- @user.id.to\_s + ‘’)’, ‘Travel Manager’,  
@user.primary\_account\_id] ).collect {|h| [h.first + " "
- h.last, h.id]}, { :include\_blank =\> true }, {:onchange =\>  
remote\_function( :update =\> ‘saved\_travel\_arrangers\_div’, :url =\>  
{:action =\>  
‘saved\_travel\_arrangers’, :id =\> @user.id},  
:with =\> “‘select=’ + escape(value)” )}) %\>

Now, the above code is in a partial which is wrraped in a div so I can  
change it:

.....

And everything seems to work the way I want - changing the two divs -  
but I get all this extra junk displayed in the page.

-S

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [August 15, 2008, 11:48pm UTC](https://www.ruby-forum.com/t/render-update/145986/4 "2008-08-15T23:48:12Z")

</div>

> just render the partial:  
> respond\_to do |format|  
> format.js { render :partial =\> ‘show\_my\_managers’}  
> end
> 
> I might be off base, sorry if I am.

I actually like that solution, I didn’t know that you could do that. The  
issue now is that I need to replace the HTML in the div I am updating  
with new HTML. Is there a way to replace the existing HTML in the div  
this way? Thanks,

-S

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [August 15, 2008, 10:17pm UTC](https://www.ruby-forum.com/t/render-update/145986/5 "2008-08-15T22:17:38Z")

</div>

Shandy N. wrote:

> Lake D. wrote:
> 
> > How are you including your javascript?
> > 
> > Lake
> 
> The action get intiated by the onchange of a dropdown, so I have:
> 
> \<%= select( “managersplususers”, “travel\_arranger\_id”,  
> User.find( :all, :conditions =\> ['role = ? and primary\_account\_id = ?  
> and id  
> NOT IN( SELECT travel\_arranger\_id FROM Managersplususers WHERE user\_id  
> = ‘’
> 
> - @user.id.to\_s + ‘’)’, ‘Travel Manager’,  
> @user.primary\_account\_id] ).collect {|h| [h.first + " "
> - h.last, h.id]}, { :include\_blank =\> true }, {:onchange =\>  
> remote\_function( :update =\> ‘saved\_travel\_arrangers\_div’, :url =\>  
> {:action =\>  
> ‘saved\_travel\_arrangers’, :id =\> @user.id},  
> :with =\> “‘select=’ + escape(value)” )}) %\>
> 
> Now, the above code is in a partial which is wrraped in a div so I can  
> change it:
> 
> .....
> 
> And everything seems to work the way I want - changing the two divs -  
> but I get all this extra junk displayed in the page.
> 
> -S

I can’t be 100% sure… but it seems like you are maybe updating your  
the div twice.  
Once in the controller:  
page.replace\_html ‘saved\_travel\_arrangers\_div’, :partial =\>  
‘show\_my\_managers’

and once in the view:  
{:onchange =\> remote\_function( :update =\> ‘saved\_travel\_arrangers\_div’,  
:url =\>  
{:action =\> ‘saved\_travel\_arrangers’, :id =\> @user.id}, :with =\>  
“‘select=’ + escape(value)” )}

So, I would suggest dropping the controller update and instead make it  
just render the partial:  
respond\_to do |format|  
format.js { render :partial =\> ‘show\_my\_managers’}  
end

I might be off base, sorry if I am.

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [August 16, 2008, 6:00am UTC](https://www.ruby-forum.com/t/render-update/145986/6 "2008-08-16T06:00:01Z")

</div>

Shandy N. wrote:

> > just render the partial:  
> > respond\_to do |format|  
> > format.js { render :partial =\> ‘show\_my\_managers’}  
> > end
> > 
> > I might be off base, sorry if I am.
> 
> I actually like that solution, I didn’t know that you could do that. The  
> issue now is that I need to replace the HTML in the div I am updating  
> with new HTML. Is there a way to replace the existing HTML in the div  
> this way? Thanks,
> 
> -S

I’ll use a concise text\_field\_tag method to explain how to update the  
div (I am assuming the div you want to update is called  
“saved\_travel\_arrangers\_div”.

# View

\<%= text\_field\_tag “Hallo!”, “#”, :onclick =\>  
remote\_function(:update=\>‘saved\_travel\_arrangers\_div’, :url =\> {:action  
=\>‘saved\_travel\_arrangers’}, :with =\>  
“‘select=’ + escape(value)”) %\>

# Controller

def saved\_travel\_arrangers  
respond\_to do |format|  
format.js {render :text =\> “#{params[:select]}”}  
end  
end

# JS Includes

\<%= javascript\_include\_tag :defaults %\>

Basically you are specifying the div you want to update with the :update  
option. Does that make sense?

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [August 18, 2008, 4:40pm UTC](https://www.ruby-forum.com/t/render-update/145986/7 "2008-08-18T16:40:02Z")

</div>

> I’ll use a concise text\_field\_tag method to explain how to update the  
> div (I am assuming the div you want to update is called  
> “saved\_travel\_arrangers\_div”.
> 
> # View
> 
> \<%= text\_field\_tag “Hallo!”, “#”, :onclick =\>  
> remote\_function(:update=\>‘saved\_travel\_arrangers\_div’, :url =\> {:action  
> =\>‘saved\_travel\_arrangers’}, :with =\>  
> “‘select=’ + escape(value)”) %\>
> 
> # Controller
> 
> def saved\_travel\_arrangers  
> respond\_to do |format|  
> format.js {render :text =\> “#{params[:select]}”}  
> end  
> end
> 
> # JS Includes
> 
> \<%= javascript\_include\_tag :defaults %\>
> 
> Basically you are specifying the div you want to update with the :update  
> option. Does that make sense?

I got it to work. The issue that I was having was that in one of my  
partials I had a table formated wrong - basically I had row and colums  
tags but not within table tags. This was allowing bizarre formating to  
occur. I did end up using the method found above, but I am still curious  
as to why the render :update didn’t work? I am using similar code on  
another page and it works fine. But this code:

render :update do |page|  
page.replace\_html ‘saved\_travel\_arrangers\_div’, :partial  
=\>‘show\_my\_managers’  
page.replace\_html ‘travel\_managers\_list’, :partial  
=\>‘list\_managers\_dropdown’  
end

renders some try catch code along with all the other wierd formatting I  
described on the initial post to this discussion. The reason why I  
initially wanted to do it using the render :update is because I needed  
to update two div within one action. Anyone have any ideas? Thanks,

-S

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [August 18, 2008, 4:44pm UTC](https://www.ruby-forum.com/t/render-update/145986/8 "2008-08-18T16:44:49Z")

</div>

On 18 Aug 2008, at 15:40, Shandy N. wrote:

> page.replace\_html ‘saved\_travel\_arrangers\_div’, :partial

That’s because you are passing :update to remote\_function. Stop doing  
that and you’ll be ok.  
the :update option means ‘stuff the response body into this page  
element’ whereas you want the response to be executed.

Fred
