trouble updating partial from collection

I have a view where I render a partial over a collection. Each instance of the partial includes a <div> 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

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

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

Frederick Cheung 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

Frederick Cheung 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?

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

Frederick Cheung wrote:

Frederick Cheung 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.

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

Frederick Cheung 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