Ruby Rails - 如何在不删除整个购物车项目的情况下删除我的购物车中的单个 ProductItem
Ruby on Rails - How can I delete a single ProductItem in my Cart without deleting the whole Cart items
过程总结
我正在尝试开发一个简单的电子商务应用程序。我创建了 Products,它变成了 Product-Items,然后这些产品项目被添加到 Cart 并且然后到 Order.
问题
一切正常如果我想删除购物车中的所有产品,请使用以下代码:
<%= link_to 'Empty Your Cart', @cart, method: :delete, data: {confirm: 'You sure?'} %>
但我似乎无法弄清楚如何在不删除所有项目的情况下删除一个项目。
我试过使用这个功能
<%= link_to 'Delete', @product_item, method: :delete, data: {confirm: 'You sure?'} %>
在我的购物车表格上:
<% @cart.product_items.each do|item| %>
<ul>
<li>
<%= item.quantity %>×<%= item.product.title %> =
<%= number_to_currency item.total_price %> - <%=item.product.size %>.
<%= link_to 'Delete', @product_items, method: :delete, data: {confirm: 'You sure?'} %>
</li>
</ul>
<% end %>
并多次更改我的控制器,但似乎没有任何效果。我确信它可能真的很简单,但我是 Rails 的新手,似乎无法弄清楚。
我的推车控制器
class CartsController < ApplicationController
before_action :set_cart, only: [:show, :create, :destroy]
rescue_from ActiveRecord::RecordNotFound, with: :invalid_cart
def new
@cart = Cart.new
end
def show
end
def destroy
@cart.destroy if @cart.id == session[:cart_id]
session[:cart_id] = nil
redirect_to root_url, notice: 'Your cart is empty'
end
private
def set_cart
@cart = Cart.find(params[:id])
end
def cart_params
params[:cart]
end
def invalid_cart
logger_error = 'You are trying to access an invalid cart'
redirect_to product_url, notice:'Invalid Cart'
end
end
我的cart.rb模特
class Cart < ApplicationRecord
has_many :product_items, dependent: :destroy
def add_product(product_id)
current_item = product_items.find_by(product_id: product_id)
if current_item
current_item.quantity += 1
else
current_item = product_items.build(product_id: product_id)
end
current_item
end
def total_price
product_items.to_a.sum{|item| item.total_price}
end
end
我认为你很接近,但你应该将销毁路线更改为特定项目。因此,将 @product_items
更改为特定项 item
,即循环内的对象。
<% @cart.product_items.each do|item| %>
<ul>
<li>
<%= item.quantity %>×<%= item.product.title %> =
<%= number_to_currency item.total_price %> - <%=item.product.size %>.
<%= link_to 'Delete', item, method: :delete, data: {confirm: 'You sure?'} %>
</li>
</ul>
<% end %>
过程总结
我正在尝试开发一个简单的电子商务应用程序。我创建了 Products,它变成了 Product-Items,然后这些产品项目被添加到 Cart 并且然后到 Order.
问题
一切正常如果我想删除购物车中的所有产品,请使用以下代码:
<%= link_to 'Empty Your Cart', @cart, method: :delete, data: {confirm: 'You sure?'} %>
但我似乎无法弄清楚如何在不删除所有项目的情况下删除一个项目。
我试过使用这个功能
<%= link_to 'Delete', @product_item, method: :delete, data: {confirm: 'You sure?'} %>
在我的购物车表格上:
<% @cart.product_items.each do|item| %>
<ul>
<li>
<%= item.quantity %>×<%= item.product.title %> =
<%= number_to_currency item.total_price %> - <%=item.product.size %>.
<%= link_to 'Delete', @product_items, method: :delete, data: {confirm: 'You sure?'} %>
</li>
</ul>
<% end %>
并多次更改我的控制器,但似乎没有任何效果。我确信它可能真的很简单,但我是 Rails 的新手,似乎无法弄清楚。
我的推车控制器
class CartsController < ApplicationController
before_action :set_cart, only: [:show, :create, :destroy]
rescue_from ActiveRecord::RecordNotFound, with: :invalid_cart
def new
@cart = Cart.new
end
def show
end
def destroy
@cart.destroy if @cart.id == session[:cart_id]
session[:cart_id] = nil
redirect_to root_url, notice: 'Your cart is empty'
end
private
def set_cart
@cart = Cart.find(params[:id])
end
def cart_params
params[:cart]
end
def invalid_cart
logger_error = 'You are trying to access an invalid cart'
redirect_to product_url, notice:'Invalid Cart'
end
end
我的cart.rb模特
class Cart < ApplicationRecord
has_many :product_items, dependent: :destroy
def add_product(product_id)
current_item = product_items.find_by(product_id: product_id)
if current_item
current_item.quantity += 1
else
current_item = product_items.build(product_id: product_id)
end
current_item
end
def total_price
product_items.to_a.sum{|item| item.total_price}
end
end
我认为你很接近,但你应该将销毁路线更改为特定项目。因此,将 @product_items
更改为特定项 item
,即循环内的对象。
<% @cart.product_items.each do|item| %>
<ul>
<li>
<%= item.quantity %>×<%= item.product.title %> =
<%= number_to_currency item.total_price %> - <%=item.product.size %>.
<%= link_to 'Delete', item, method: :delete, data: {confirm: 'You sure?'} %>
</li>
</ul>
<% end %>