# Trouble updating partial from collection

**URL:** <https://www.ruby-forum.com/t/trouble-updating-partial-from-collection/128986>\
**Category:** Rails\
**Created:** [February 13, 2008, 9:04am UTC](https://www.ruby-forum.com/t/trouble-updating-partial-from-collection/128986 "2008-02-13T09:04:24Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeffpritchard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/43a26b/32.png) [@jeffpritchard](https://www.ruby-forum.com/u/jeffpritchard)\
**Post date:** [February 13, 2008, 9:04am UTC](https://www.ruby-forum.com/t/trouble-updating-partial-from-collection/128986/1 "2008-02-13T09:04:24Z")

</div>

I have a view where I render a partial over a collection. Each instance  
of the partial includes a

 with an id specific to that instance.

Inside the partial, there is a select tag for “year”. I’m trying to  
change some of the contents of the partial when user changes the year.  
I’m using this in the view:

```
    <%= select_tag :year,
                    options_for_select(recent_years, @year),
                    :onchange => remote_function( :url => {:update

```

=\> dom\_id(product),  
:action =\>  
‘year\_selection\_changed’,  
:product\_id  
=\> product.id,  
:company\_id  
=\> @company.id,  
:update\_div\_id  
=\> dom\_id(product) },  
:with =\>  
“‘year=’+value”)  
-%\>

that dom\_id(product) is a helper that gives the div a unique “id”. The  
same helper is used to give the div its unique id, and in the code above  
to specify which part of the page we are going to update.

In my controller:  
def year\_selection\_changed  
…  
render :update do |page|  
page.replace\_html params[:update\_div\_id], :partial =\> ‘product’,  
:object =\> @product  
end  
end

The problem is that instead of replacing the content of the div in  
question, it just adds the newly rendered stuff above what was already  
there. It is acting the same as it would if you changed the part of the  
select code:  
:url =\> {:update =\> dom\_id(product),  
to something that doesn’t exist:  
:url =\> {:update =\> “fred”,

Anybody know how to fix this thing so it will replace the div instead of  
adding to it?

many thanks,  
jp

---

<div class="post-metadata">

**Author:** ![jeffpritchard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/43a26b/32.png) [@jeffpritchard](https://www.ruby-forum.com/u/jeffpritchard)\
**Post date:** [February 13, 2008, 9:43am UTC](https://www.ruby-forum.com/t/trouble-updating-partial-from-collection/128986/2 "2008-02-13T09:43:02Z")

</div>

On 13 Feb 2008, at 08:04, Jeff P. wrote:

> ```
> page.replace_html params[:update_div_id], :partial => 'product',
> 
> ```
> 
> :object =\> @product  
> end  
> end

At least part of the problem is that you are doing render :update =\>  
‘…’ which means ‘please insert the result of this ajax call into  
this div’ and you are doing a render :update (ie you are generating  
javascript to replace some html).

What happens if you change your controller so that instead it just  
renders the appropriate partial (ie year\_selection\_changed.html.erb is  
just \<%= render :partial =\> ‘product’ %\>

Fred

---

<div class="post-metadata">

**Author:** ![jeffpritchard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/43a26b/32.png) [@jeffpritchard](https://www.ruby-forum.com/u/jeffpritchard)\
**Post date:** [February 13, 2008, 9:59am UTC](https://www.ruby-forum.com/t/trouble-updating-partial-from-collection/128986/3 "2008-02-13T09:59:26Z")

</div>

Frederick C. wrote:

> On 13 Feb 2008, at 08:04, Jeff P. wrote:
> 
> > ```
> > page.replace_html params[:update_div_id], :partial => 'product',
> > 
> > ```
> > 
> > :object =\> @product  
> > end  
> > end
> 
> At least part of the problem is that you are doing render :update =\>  
> ‘…’ which means ‘please insert the result of this ajax call into  
> this div’ and you are doing a render :update (ie you are generating  
> javascript to replace some html).
> 
> What happens if you change your controller so that instead it just  
> renders the appropriate partial (ie year\_selection\_changed.html.erb is  
> just \<%= render :partial =\> ‘product’ %\>
> 
> Fred

Wow, I must be clairvoyant this evening, how did I know to use “fred” as  
my unknown div id?

Anyway, wouldn’t it need to be:  
\<%= render :partial =\> ‘product’, :object =\> @product %\>

so the partial knows which product to render?

BTW, my take on the render :update thing is that it is acting like it  
doesn’t recognized the div id in the :update parameter of the URL. With  
it being a “remote\_function” call, I was thinking one would need to do  
an ajax render :update rather than a simple render. I reckon if it  
recognized the div’s id and actually did the :update that was  
requested, then it would indeed need just the render :partial…

Anyway, as you can see, this stuff has always confused me somewhat.

thanks,  
jp

---

<div class="post-metadata">

**Author:** ![jeffpritchard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/43a26b/32.png) [@jeffpritchard](https://www.ruby-forum.com/u/jeffpritchard)\
**Post date:** [February 13, 2008, 10:16am UTC](https://www.ruby-forum.com/t/trouble-updating-partial-from-collection/128986/4 "2008-02-13T10:16:35Z")

</div>

On 13 Feb 2008, at 08:59, Jeff P. wrote:

> > At least part of the problem is that you are doing render :update =\>
> 
> Wow, I must be clairvoyant this evening, how did I know to use  
> “fred” as  
> my unknown div id?
> 
> Anyway, wouldn’t it need to be:  
> \<%= render :partial =\> ‘product’, :object =\> @product %\>
> 
> so the partial knows which product to render?

If you don’t tell it what to render it will try @product since the  
partial is called product

> BTW, my take on the render :update thing is that it is acting like it  
> doesn’t recognized the div id in the :update parameter of the URL.  
> With  
> it being a “remote\_function” call, I was thinking one would need to do  
> an ajax render :update rather than a simple render. I reckon if it  
> recognized the div’s id and actually did the :update that was  
> requested, then it would indeed need just the render :partial…

the fundamental thing to remember is that link\_to\_remote :update =\>  
‘foo’ says ‘dump the body of the response in the div with id foo’  
and render :update do |page|  
…  
end  
generates javascript. With that in mind it should be obvious what is  
what.

Fred

---

<div class="post-metadata">

**Author:** ![jeffpritchard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/43a26b/32.png) [@jeffpritchard](https://www.ruby-forum.com/u/jeffpritchard)\
**Post date:** [February 13, 2008, 10:43am UTC](https://www.ruby-forum.com/t/trouble-updating-partial-from-collection/128986/5 "2008-02-13T10:43:03Z")

</div>

On 13 Feb 2008, at 09:28, Jeff P. wrote:

> So with this in there, and supplying the @product as I mentioned in my  
> last reply, it does precisely nothing, and the log says it did the  
> expected render. Just nothing happens on the screen at all.

Install firebug and you’ll be able to see exactly what the browser is  
sending and exactly what is being returned by your app.

---

<div class="post-metadata">

**Author:** ![jeffpritchard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/43a26b/32.png) [@jeffpritchard](https://www.ruby-forum.com/u/jeffpritchard)\
**Post date:** [February 13, 2008, 10:28am UTC](https://www.ruby-forum.com/t/trouble-updating-partial-from-collection/128986/6 "2008-02-13T10:28:37Z")

</div>

Frederick C. wrote:

> On 13 Feb 2008, at 08:04, Jeff P. wrote:  
> What happens if you change your controller so that instead it just  
> renders the appropriate partial (ie year\_selection\_changed.html.erb is  
> just \<%= render :partial =\> ‘product’ %\>
> 
> Fred

So with this in there, and supplying the @product as I mentioned in my  
last reply, it does precisely nothing, and the log says it did the  
expected render. Just nothing happens on the screen at all.

thanks,  
jp

---

<div class="post-metadata">

**Author:** ![jeffpritchard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/43a26b/32.png) [@jeffpritchard](https://www.ruby-forum.com/u/jeffpritchard)\
**Post date:** [February 13, 2008, 11:31am UTC](https://www.ruby-forum.com/t/trouble-updating-partial-from-collection/128986/7 "2008-02-13T11:31:11Z")

</div>

Frederick C. wrote:

> On 13 Feb 2008, at 09:28, Jeff P. wrote:
> 
> > So with this in there, and supplying the @product as I mentioned in my  
> > last reply, it does precisely nothing, and the log says it did the  
> > expected render. Just nothing happens on the screen at all.
> 
> Install firebug and you’ll be able to see exactly what the browser is  
> sending and exactly what is being returned by your app.

Thanks for the help guys, firebug showed everything working as expected,  
and still nothing happening on the screen.

Stared at it some more and looked at some other times I have used this  
and finally realized that my “:update” was erroneously tucked inside the  
:url hash.

pulled that out and now its working fine.

thanks,  
jp
