简体   繁体   中英

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

<%  @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

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:

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...

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. Therefore @item = OrderItem.find(params[:id]) has not been executed yet. Move the break point one line lower or step one statement further.

Furthermore you're passing a wrong argument to the form_for helper. 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.

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.

{
  "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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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