我正在使用购物车构建应用。但是,我无法从购物车中删除商品。 Items,Line_items和Carts有资源。他们有这种关系:
class Cart < ActiveRecord::Base
has_many :line_items
has_one :order
end
class LineItem < ActiveRecord::Base
belongs_to :cart
belongs_to :item
end
我可以像这样在购物车中添加商品:
<% for item in @items %>
<%= link_to "Add to Cart", line_items_path(:item_id => item), :method => :post, class: "btn" %>
<% end %>
这将调用line_items控制器中的create方法:
def create
@item = Item.find(params[:item_id])
@line_item = LineItem.create!(:cart => current_cart, :item => @item, :quantity => 1)
flash[:notice] = "Added #{@item.name} to sample."
redirect_to root_path
end
以下是问题的要点: 我无法弄清楚如何从购物车中删除东西。 我可以通过运行以下命令删除控制台中的line_item:
cart = Cart.last
cart.line_items.destroy(9)
如何在应用中实现此功能?这种方法的控制器是什么? 我正在努力让这个工作:
<% for line_item in @cart.line_items %>
<%= link_to 'Remove', line_item, method: :delete %>
<% end %>
我已经尝试在carts_controller和line_items_controller中定义它,但无济于事。 我的基本问题是:如何将某些内容从控制台转换为应用文件?
以下是路线:
line_items GET /line_items(.:format) line_items#index
POST /line_items(.:format) line_items#create
new_line_item GET /line_items/new(.:format) line_items#new
edit_line_item GET /line_items/:id/edit(.:format) line_items#edit
line_item GET /line_items/:id(.:format) line_items#show
PATCH /line_items/:id(.:format) line_items#update
PUT /line_items/:id(.:format) line_items#update
DELETE /line_items/:id(.:format) line_items#destroy
items GET /items(.:format) items#index
POST /items(.:format) items#create
new_item GET /items/new(.:format) items#new
edit_item GET /items/:id/edit(.:format) items#edit
item GET /items/:id(.:format) items#show
PATCH /items/:id(.:format) items#update
PUT /items/:id(.:format) items#update
DELETE /items/:id(.:format) items#destroy
orders GET /orders(.:format) orders#index
POST /orders(.:format) orders#create
new_order GET /orders/new(.:format) orders#new
edit_order GET /orders/:id/edit(.:format) orders#edit
order GET /orders/:id(.:format) orders#show
PATCH /orders/:id(.:format) orders#update
PUT /orders/:id(.:format) orders#update
DELETE /orders/:id(.:format) orders#destroy
root GET / items#index
thanks GET /thanks(.:format) pages#thanks
DELETE /carts/:id(.:format) cart#remove_item
carts GET /carts(.:format) carts#index
POST /carts(.:format) carts#create
new_cart GET /carts/new(.:format) carts#new
edit_cart GET /carts/:id/edit(.:format) carts#edit
cart GET /carts/:id(.:format) carts#show
PATCH /carts/:id(.:format) carts#update
PUT /carts/:id(.:format) carts#update
答案 0 :(得分:1)
<强>破坏强>
您问题的直接答案是将其置于destroy
控制器的line_items
操作中:
<% for line_item in @cart.line_items %>
<%= link_to 'Remove', line_item, method: :delete %> #-> will take you to destroy path
<% end %>
这将带您:
#app/controllers/line_items_controller.rb
Class LineItemsController < ApplicationController
def destroy
@line_item = LineItem.find params[:id]
@line_item.destroy
end
end
-
<强>集合强>
处理问题的更有效方法是从collection
删除记录。这需要更多的努力,但是完全值得:
#config/routes.rb
resources :cart, except: :destroy do
collection do
delete ":id", to: "cart#remove_item" #-> DELETE domain.com/cart/56
end
end
#app/controllers/cart_controller.rb
class CartController < ApplicationController
def remove_item
current_cart.line_items.delete params[:id]
end
end
答案 1 :(得分:0)
你在line_items控制器中有一个destroy方法。该网址将与您编码的一样......
<%= link_to 'Remove', line_item, method: :delete %>
我建议使用button_to&#39;添加到购物车&#39;如果你想让它们看起来像按钮。
单击它应该调用正确的控制器和操作。
class LineItemsController < ApplicationController
def destroy
@line_item = LineItem.find(params[:id])
@line_item.destroy
# Then redirect to the index or render the js view
end
end