title: Simple quantity function problems, shopping cart
body: <p>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. </p>
  
  <p>I'm going to provide a lot of information, so please don't feel overwhelmed. I've found this to be a challenging problem.</p>
  
  <p><strong>To add_to_cart.html.erb</strong></p>
  
  <pre><code>&lt;div class="cart-title"&gt;Your cart&lt;/div&gt;
  
  &lt;table&gt;
  &lt;% for item in @cart.items %&gt;
  &lt;tr&gt;
  
  &lt;td&gt;&lt;% form_for @cart.items, :url =&gt; {:action =&gt; "cart_update", :id =&gt; "#{item.getinventoryid}"} do |f| %&gt;
    &lt;%= f.text_field :quantity, :size =&gt; '3' %&gt;
    &lt;%= f.hidden_field :id, :value =&gt; "#{item.getinventoryid}" %&gt;
    &lt;%= f.submit 'cart_update' %&gt;
  &lt;% end %&gt;&lt;/td&gt;
  
  &lt;td&gt;&lt;%=h item.quantity %&gt; &amp;times;&lt;/td&gt;
  &lt;td&gt;&lt;%=h item.title %&gt;&lt;/li&gt;&lt;/td&gt;
  &lt;td&gt;&lt;%=h item.description %&gt;&lt;/td&gt;
  &lt;td class="item-price"&gt;&lt;%= number_to_currency(item.price) %&gt;&lt;/td&gt;
  &lt;td&gt;&lt;%= link_to 'remove', {:controller =&gt; 'inventories', :action =&gt; 'remove_cart_item', :id =&gt; "#{item.getinventoryid}"} %&gt;&lt;/td&gt;
  
  
  &lt;/tr&gt;
  &lt;% end %&gt;
  
  &lt;tr class="total-line"&gt;
  &lt;td colspan="4"&gt;Total&lt;/td&gt;
  &lt;td class="total-cell"&gt;&lt;%= number_to_currency(@cart.total_price) %&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;/table&gt;
  
  &lt;%= button_to "Checkout", :action =&gt; 'checkout' %&gt;
  &lt;%= button_to 'Empty cart', :action =&gt; 'empty_cart' %&gt;
  </code></pre>
  
  <p><strong>inventories_controller:</strong> </p>
  
  <pre><code>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
  </code></pre>
  
  <p><strong>Cart.rb model</strong></p>
  
  <p><code>attr_accessor :items</code></p>
  
  <pre><code>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
  </code></pre>
  
  <p><strong>cart_item.rb model</strong></p>
  
  <p><code>attr_accessor :inventory, :quantity</code></p>
  
  <pre><code>def getinventoryid
          @inventory.id
      end
  </code></pre>
  
  <p>This produces strange results:
  <img src="https://farm3.static.flickr.com/2755/4282358599_3b997882a8_o.png" alt="alt text"></p>
  
  <p>Notice the quantity <strong>16</strong> appears in both items from my loop (#Fail). When I submit the form a ArgumentError in InventoriesController#<strong>cart_update</strong> wrong number of arguments (1 for 2) error is returned. Parameters being passed:</p>
  
  <pre><code>{"commit"=&gt;"cart_update",
   "_method"=&gt;"put",
   "authenticity_token"=&gt;"sH1tWXTJPltpSq5XaAkww7259IR5ZiflnqSFB2Zb0IY=",
   "id"=&gt;"50",
   "cart_item"=&gt;{"quantity"=&gt;"16",
   "id"=&gt;"50"}} 
  </code></pre>