ruby on rails - Page not updating when using jquery/ajax -
i'm making web app lets people add courses shopping cart. have cart model has_many lineitems.
in cart, have ajaxified button lets people remove individual line items. works fine. here's code that:
_line_item.html.erb:
<div class="delete_line_item"><%= button_to 'delete', line_item, {:method => :delete, :remote => true} %></div>
the line_items controller:
def destroy @user = current_user @line_item.destroy @cart = @user.cart respond_to |format| format.html { redirect_to(root_path) } format.js format.xml { head :ok } end end
then, destroy.js.erb file, located within views/line_items folder:
$("#cart").html("<%= escape_javascript(render(@cart)) %>");
i want similar emptying out cart. this, i'm using update action in carts controller. here corresponding code chunks this:
<div class="empty_cart"><%= button_to 'empty cart', cart, {:method => :put, :remote => true, :confirm => 'are sure?'} %></div> #carts controller def update if @cart.line_items @cart.line_items.each |item| item.destroy end end respond_to |format| if @cart.update_attributes(params[:cart]) format.html { redirect_to(root_path, :notice => 'cart emptied.') } debugger format.js format.xml { head :ok }
finally, here's update.js.erb file i've placed within views/carts folder:
$("#cart").html("<%= escape_javascript(render(@cart)) %>");
the problem when try empty cart, request goes through (in when refresh page, cart empty), change doesn't appear upon clicking. makes me think browser not rendering .js.erb file. need place update.js.erb file in different folder or rename it? or else going on?
instead of using update action, why don't use destroy action cart instead of using update? that's did, , worked.
Comments
Post a Comment