Rails 3 ajax update partial doesnt work without page refresh -
i'm working on rails 3 shopping cart. i've catalog page displays products , 1 partial gives gist of shopping cart. whatever product user adds cart displayed in shopping cart partial. now, ain't working. i've hit refresh after adding product cart, otherwise partial doesn't update.
here's code:
cart_controller add method
def add
@cart = user_cart
@deal = deal.find(params[:id])
@item = @cart.add(params[:id])
flash[:notice] = "added #{@deal.deal_name} cart"
@cart = user_cart
respond_to |format|
format.html{ redirect_to root_path}
format.js
end
add.js.erb
$("#shopping_cart").replace_html("<%=escape_javascript(render(:partial => 'cart', :locals => {:cart => @cart}))%>");
products page
<div id="shopping_cart">
<%=render :partial => 'cart/cart',
:locals => {:cart => user_cart}%>
<%end%>
</div>
<%=link_to "add cart",{:action => 'add', :id => deal.id}, :remote => true%>
this see in logs:
started "/cart/add/2" 127.0.0.1 @ 2011-07-02 18:04:38 -0700
processing cartcontroller#add js
parameters: {"id"=>"2"}
rendered cart/_cart.html.erb (9.6ms)
rendered cart/add.js.erb (11.7ms)
completed 200 ok in 274ms (views: 23.4ms | activerecord: 0.0ms)
please me this.
try
$("#shopping_cart").html("<%= escape_javascript(render(:partial => 'cart', :locals => {:cart => @cart})).html_safe %>");
Comments
Post a Comment