簡體   English   中英

Rails:商品未從購物車中移除

[英]Rails: Item is not removed from cart

我正在使用帶購物車和line_items的訂購應用程序。 刪除購物車中的line_item時遇到問題。 單擊刪除項目后,什么也沒有發生。 誰能告訴我我要去哪里錯了? 我的cart.html.erb訂單中

<% @order.line_items.each do |item| %>
      <%= link_to "remove item", item, :method => :delete, :confirm => "Are you sure?",:remote => true %>
<% end %>

我的訂單控制器有:

def cart
    @order = current_or_guest_user.orders.includes(:line_items=>[:product]).last
end

我已經在line_items控制器中定義了刪除項方法:

 def destroy
             line_item.destroy
             redirect_to cart_orders_path
 end

訂單模型為:

belongs_to :user
attr_accessible :completed_at, :email, :item_total, :number, :payment_state, :payment_total, :special_instructions, :state, :total
has_many :line_items, :dependent => :destroy

訂單項模型為:

belongs_to :product
belongs_to :order
attr_accessible :price, :quantity, :product_id

誰能幫我?

您的代碼中存在多個不一致之處。 首先,我不知道您是否只是丟失了一段代碼,但沒有加載要刪除的line_item:

def destroy
         @line_item = LineItem.find(params[:id])
         @line_item.destroy
         redirect_to cart_orders_path
end

其次,在銷毀鏈接上指定:remote => true,這將打開AJAX模式,但隨后您只需在銷毀操作中重定向即可。

首先找到您要銷毀的對象

def destroy
  line_item = LineItem.find(params[:id])
  line_item.destroy
  redirect_to cart_orders_path
end
  1. 您有line_item.destroy的路線嗎? 請告訴我們您的路線。
  2. 當您按下“刪除項目”時,您會調用銷毀方法嗎? 做類似的事情
    \n def破壞\n   打印“調用行項目銷毀方法”\n   line_item.destroy\n   redirect_to cart_orders_path\n 結束\n
    然后嘗試單擊“刪除項目”鏈接。 服務器日志中是否打印了字符串“調用了行項目銷毀方法”?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM