Search code examples
ruby-on-railsrubyshopping-cartagile

Simple quantity function problems, shopping cart


I just picked up Agile Web Development with Rails 3rd Ed., and I'm going thru the Depot Application chapters, I'm attempting to create a simple Edit quantity function, and delete function. I've had luck with the delete function but no luck with the edit quantity function.

I'm going to provide a lot of information, so please don't feel overwhelmed. I've found this to be a challenging problem.

To add_to_cart.html.erb

<div class="cart-title">Your cart</div>

<table>
<% for item in @cart.items %>
<tr>

<td><% form_for @cart.items, :url => {:action => "cart_update", :id => "#{item.getinventoryid}"} do |f| %>
  <%= f.text_field :quantity, :size => '3' %>
  <%= f.hidden_field :id, :value => "#{item.getinventoryid}" %>
  <%= f.submit 'cart_update' %>
<% end %></td>

<td><%=h item.quantity %> &times;</td>
<td><%=h item.title %></li></td>
<td><%=h item.description %></td>
<td class="item-price"><%= number_to_currency(item.price) %></td>
<td><%= link_to 'remove', {:controller => 'inventories', :action => 'remove_cart_item', :id => "#{item.getinventoryid}"} %></td>


</tr>
<% end %>

<tr class="total-line">
<td colspan="4">Total</td>
<td class="total-cell"><%= number_to_currency(@cart.total_price) %></td>
</tr>
</table>

<%= button_to "Checkout", :action => 'checkout' %>
<%= button_to 'Empty cart', :action => 'empty_cart' %>

inventories_controller:

def cart_update
        @inventory = Inventory.find(params[:id])
        @cart = find_cart
        @cart.increment_inventory_quantity(params[:inventory])
    end

    def remove_cart_item
        inventory = Inventory.find(params[:id])
        @cart = find_cart
        @cart.remove_inventory(inventory)
        redirect_to_index("The item was removed")
    end

Cart.rb model

attr_accessor :items

def increment_inventory_quantity(id, quantity)
   inventory_to_increment = @items.select{|item| item.inventory == inventory}

   # We do this because select will return an array
   unless product_to_increment.empty?
      inventory_to_increment = inventory_to_increment.first
   else
      # error handling here
   end

   inventory_to_increment.quantity = quantity
end

def remove_inventory(inventory)
   @items.delete_if {|item| item.inventory == inventory }
end

cart_item.rb model

attr_accessor :inventory, :quantity

def getinventoryid
        @inventory.id
    end

This produces strange results: alt text

Notice the quantity 16 appears in both items from my loop (#Fail). When I submit the form a ArgumentError in InventoriesController#cart_update wrong number of arguments (1 for 2) error is returned. Parameters being passed:

{"commit"=>"cart_update",
 "_method"=>"put",
 "authenticity_token"=>"sH1tWXTJPltpSq5XaAkww7259IR5ZiflnqSFB2Zb0IY=",
 "id"=>"50",
 "cart_item"=>{"quantity"=>"16",
 "id"=>"50"}} 

Solution

  • You are getting the wrong number of arguments error because you are passing one argument to @cart.increment_inventory_quantity in the controller method. That method requires two arguments.

    # In your controller:
    def cart_update
      @inventory = Inventory.find(params[:id])
      @cart = find_cart
      @cart.increment_inventory_quantity(params[:inventory]) # you are passing one thing
    end
    # Then in your model:
    def increment_inventory_quantity(id, quantity) # the method wants two things
      # ...
    

    Possibly you intended to do something like this:

    def cart_update
      @inventory = Inventory.find(params[:cart_item][:id])
      @cart = find_cart
      @cart.increment_inventory_quantity(@inventory.id, params[:cart_item][:quantity])
    end