[英]Rails - Shopping Cart - How to increase quantity for a specific item in the current cart
我的用戶選擇了一個產品。
現在他在購物車中,他最終想要其中兩個
因此,在購物車中有一種增加數量的形式
ORDER_ITEMS / index.html.erb
<% @items.each do |item| %>
<%= image_tag(item.product.attachments.first.url, class: "tiny_image") %>
<%= link_to item.product.title, clients_product_path(item.product_id), class: "title_in_tab" %>
<%= item.quantity %>
<%= number_to_currency_euro item.product.price %>
<%= item.size.size_name %>
<%= form_for edit_clients_order_item_path(item), method: :patch, remote: true do |f| %>
<%= f.hidden_field :id, value: item.id %>
<%= f.hidden_field :product_id, value: item.product.id %>
<%= f.hidden_field :size_id, value: item.size.id %>
<%= f.select :quantity, [1,2,3,4,5] %>
<%= f.submit "Modifier" %>
<% end %>
<%= link_to clients_order_item_path(item), method: :delete, remote: true ,data: {confirm: "Voulez vous vraiment supprimer cet article?"} do %>
<i class="fa fa-trash"></i>
<% end %>
在shopping_car.rb中
我有這種方法來增加數量
def inscrease_item(id:, quantity:1, product_id:, size_id:, user_id:, order_id:)
@size = Size.find_by(id: size_id)
@order_item = order.items.find_by(product_id: product_id, size_id: size_id)
@order_item.quantity = quantity.to_i
@order_item.save
update_sub_total!
@size.quantity -= quantity.to_i
@size.save
end
在order_items_controller.rb中,我有:
def edit
@item = OrderItem.find(params[:id])
end
def update
binding.pry
@item = current_cart
current_cart.inscrease_item(
id: params[:id],
order_id: params[:order_id],
product_id: params[:product_id],
quantity: params[:quantity],
user_id: params[:user_id],
size_id: params[:size_id])
end
private
def order_item_params
params.require(:order_item).permit(:id, :product_id, :user_id, :quantity, :size_id, :order_id)
end
我在更新方法中添加了一個斷點:
@item返回nil,我不知道怎么了...
這就是返回綁定撬
30: def update
31: binding.pry
=> 32: @item = OrderItem.find(params[:id])
33: @item.inscrease_item(
34: order_item_params
35: )
36: end
[1] pry(#<Clients::OrderItemsController>)> @item
=> nil
[2] pry(#<Clients::OrderItemsController>)> params
=> <ActionController::Parameters {"utf8"=>"✓", "_method"=>"patch", "/clients/cart/items/111/edit"=>{"id"=>"111", "product_id"=>"19", "size_id"=>"41", "quantity"=>"4"}, "commit"=>"Modifier", "controller"=>"clients/order_items", "action"=>"update"} permitted: false>
尚未設置@item
因為側面的箭頭指向要執行的下一條語句。 因此,@ @item = OrderItem.find(params[:id])
尚未執行。 將斷點下移一行或進一步執行第一條語句。
此外,您將錯誤的參數傳遞給form_for
助手。 第一個參數應該是代表對象的記錄或符號/字符串。 您可能要使用form_with
幫助器,而不是使用:url
選項。
最后但並非最不重要的一點,如您在params中(從pry輸出中看到的)所看到的,當前數據存在,但嵌套在params哈希中。
{
"utf8" => "✓",
"_method" => "patch",
"/clients/cart/items/111/edit" => {
"id" => "111",
"product_id" => "19",
"size_id" => "41",
"quantity" => "4"
},
"commit" => "Modifier",
"controller" => "clients/order_items",
"action" => "update"
}
數據也應該這樣訪問。
params['/clients/cart/items/111/edit'][:id] #=> "111"
# ^ this is due to the wrong first argument
# of the form_for helper
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.