views:

46

answers:

1

I have a very simple cart which displays the item quantity in a text_field, and the item name. I can fetch the proper data, I can't pass both of my arguments into my method (inventory ID and quantity). I'm receiving an argument error in my cart_update method of the inventories controller.

add_to_cart.html.erb

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

cart_item.rb model

attr_accessor :inventory, :quantity

    def initialize( inventory )
        @inventory = inventory
        @quantity = 1
    end

cart.rb model

attr_accessor :items

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

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

   inventory_to_increment.quantity = quantity
end

inventories_controller.rb

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

The error reads: ArgumentError in InventoriesController#cart_update

inventories_controller.rb:24:in increment_inventory_quantity' inventories_controller.rb:24:incart_update'

With the following Parameters:

{"commit"=>"cart_update",
 "quantity"=>"3",
 "_method"=>"put",
 "authenticity_token"=>"CK6to1uGWPszjz8vfL1Er3DLgi8cIMtLxfFb49a4wCE=",
 "id"=>"5",
 "cart_item"=>{"quantity"=>"11",
 "id"=>"5"}}

I'm really confused because it appears I'm passing the parameters I need. Additionally the url is formatted as: http://localhost:3000/inventories/cart_update/5?quantity=3 which is also what I was expecting to see. What am I doing that is obviously wrong?

Thanks in Advance!

A: 

Just change line 3 of cart_update, and use this instead:

if(params[:cart_item] && params[:cart_item][:id])
    @cart.increment_inventory_quantity(params[:cart_item][:id], params[:cart_item][:quantity])
else
    # Error handling
end

You can see from your POST data:

{ ... ,
  "cart_item"=>{"quantity"=>"11", "id"=>"5"}
}

That rails is nesting the cart_item details into its own hash.

Doug Neiner
I followed your fix: Returned is: undefined method `quantity=' for []:Arraywith these params:Parameters:{"commit"=>"cart_update", "_method"=>"put", "authenticity_token"=>"CK6to1uGWPszjz8vfL1Er3DLgi8cIMtLxfFb49a4wCE=", "id"=>"5", "cart_item"=>{"quantity"=>"1", "id"=>"5"}}
JZ
@JZ I assumed `#error handling here` meant you had error handling there. Your `select` statement is failing, so it returns an empty array. Your script does nothing to the array, so your last line of `increment_inventory_quantity` is effectively running this `[].quantity = 1` which will of course fail.
Doug Neiner
Thanks! my #error handling tag is a reminder to myself to add it later on.
JZ