所以我有@line_items,属于@cart。我想在不刷新页面的情况下清空我的购物车。我在购物车模板中设置了 button_to
<%= button_to 'Empty cart' , cart, :remote => true, :method => :delete,
:confirm => 'Are you sure?' %>
carts_controller 中的销毁方法
def destroy
@cart = current_cart
@cart.line_items.destroy
session[:cart_id] = nil
respond_to do |format|
format.html { redirect_to(store_url, :notice => 'Your cart is currently empty' ) }
format.js
format.xml { head :ok }
end
end
和destroy.js.erb
$('#cart').html("<%= escape_javascript(render(@cart)) %>");
仅在刷新后有效。我想,当按下按钮时,购物车被清空,所以当 rails 呈现 @cart 时,它应该显示为空。也许我做错了什么?