简体   繁体   English

Rails-购物车-如何增加当前购物车中特定项目的数量

[英]Rails - Shopping Cart - How to increase quantity for a specific item in the current cart

My user has selected a product. 我的用户选择了一个产品。

Now he is in his shopping cart and he finally wants two of this item 现在他在购物车中,他最终想要其中两个

So in the cart there is a form to increase the quantity 因此,在购物车中有一种增加数量的形式

order_items/index.html.erb 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 %>   

in the shopping_car.rb shopping_car.rb中

I have this method to inscrease the quantity 我有这种方法来增加数量

 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

in order_items_controller.rb I have: 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

I added a break point in the update method: 我在更新方法中添加了一个断点:

@item returns nil and I don't know what is wrong... @item返回nil,我不知道怎么了...

this is what returns binding pry 这就是返回绑定撬

 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 isn't set yet because the arrow on the side points to the next statement to execute. 尚未设置@item因为侧面的箭头指向要执行的下一条语句。 Therefore @item = OrderItem.find(params[:id]) has not been executed yet. 因此,@ @item = OrderItem.find(params[:id])尚未执行。 Move the break point one line lower or step one statement further. 将断点下移一行或进一步执行第一条语句。

Furthermore you're passing a wrong argument to the form_for helper. 此外,您将错误的参数传递给form_for助手。 The first argument should be an record or symbol/string representing the object. 第一个参数应该是代表对象的记录或符号/字符串。 You might want to use the form_with helper instead using the :url option. 您可能要使用form_with帮助器,而不是使用:url选项。

Last but not least, as you can see in your params (from your pry output), currently the data is present, but nested inside the params hash. 最后但并非最不重要的一点,如您在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"
}

The data should also be accessed like such. 数据也应该这样访问。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM