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

Ruby on rails Rails-购物车-如何增加当前购物车中特定项目的数量,ruby-on-rails,Ruby On Rails,我的用户选择了一个产品 现在他在他的购物车里,他终于想要两个这样的东西 因此,在购物车中有一种增加数量的形式 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_

我的用户选择了一个产品

现在他在他的购物车里,他终于想要两个这样的东西

因此,在购物车中有一种增加数量的形式

 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\u 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 %>   
订单项目\u控制器.rb中 我有:

我在更新方法中添加了一个断点:

@item返回零,我不知道怎么了

这是什么回报绑定撬

 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>
30:def更新
31:binding.pry
=>32:@item=OrderItem.find(参数[:id])
33:@item.inscrease\u项目(
34:订单项目参数
35:   )
36:完
[1] 撬(#)>@item
=>零
[2] 撬动(#)>参数
=> "✓", "_方法“=>”补丁“,“/clients/cart/items/111/编辑“=>”{“id”=>“111”,“product\u id”=>“19”,“size\u id”=>“41”,“quantity”=>“4”},“commit”=>“Modifier”,“controller”=>“clients/order\u items”,“action”=>“update”}允许:false>

@item
尚未设置,因为旁边的箭头指向要执行的下一条语句。因此,
@item=OrderItem.find(params[:id])
尚未执行。将断点下移一行或进一步移动一步语句

此外,您将错误的参数传递给助手。第一个参数应该是表示对象的记录或符号/字符串。您可能希望使用帮助程序,而不是使用
: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
params['/clients/cart/items/111/edit'][:id] #=> "111"
#           ^ this is due to the wrong first argument 
#             of the form_for helper