title: Adding Cart items Ruby on Rails
body: <p>I am developing a very basic shopping cart and the issue I am having is that if I add more than one of the same product to my shopping cart I am not seeing the quantity increase and am instead just seeing multiple versions of the item. </p>
  
  <p>For example I see:</p>
  
  <p>1 x green light = £15
  1 x green light = £15</p>
  
  <p>rather than seeing:</p>
  
  <p>2 x green light = £30</p>
  
  <p>How can I make it so that my shopping cart will check for multiple versions in the cart and then add them together?</p>
  
  <p>Currently I have: </p>
  
  <p>Application Controller</p>
  
  <pre><code>  def current_cart
    if session[:cart_id]
      @current_cart ||= Cart.find(session[:cart_id])
    end
    if session[:cart_id].nil?
      @current_cart = Cart.create!
      session[:cart_id] = @current_cart.id
    end
    @current_cart
    end 
  </code></pre>
  
  <p>Cart Controller</p>
  
  <pre><code>   def show
      @cart = current_cart
     end
  </code></pre>
  
  <p>Cart Model</p>
  
  <pre><code> has_many :items
  
   def total_price
     items.to_a.sum(&amp;:full_price)
   end
  </code></pre>
  
  <p>Cart View</p>
  
  <pre><code> &lt;table id="line_items"&gt;
     &lt;tr&gt;
       &lt;th&gt;Product&lt;/th&gt;
       &lt;th&gt;Qty&lt;/th&gt;
       &lt;th class="price"&gt;Unit Price&lt;/th&gt;
       &lt;th class="price"&gt;Full Price&lt;/th&gt;
     &lt;/tr&gt;
  
    &lt;% for item in @cart.items %&gt;
      &lt;tr class="&lt;%= cycle :odd, :even %&gt;"&gt;
         &lt;td&gt;&lt;%=h item.product.name %&gt;&lt;/td&gt;
         &lt;td class="qty"&gt;&lt;%= item.quantity %&gt;&lt;/td&gt;
         &lt;td class="price"&gt;&lt;%= gbp(item.unit_price) %&gt;&lt;/td&gt;
         &lt;td class="price"&gt;&lt;%= gbp(item.full_price) %&gt;&lt;/td&gt;
      &lt;/tr&gt;
     &lt;% end %&gt;
   &lt;tr&gt;
  
    &lt;td class="total price" colspan="4"&gt;
      Total: &lt;%= gbp(@cart.total_price) %&gt;
    &lt;/td&gt;
    &lt;/tr&gt;
    &lt;/table&gt;
  </code></pre>
  
  <p><strong>FURTHER INFORMATION</strong></p>
  
  <p>Products#Index</p>
  
  <pre><code>  &lt;%= link_to "Add to Cart", line_items_path(:product_id =&gt; product), :method =&gt; :post %&gt;
  </code></pre>
  
  <p>Any advice people can offer would be much appreciated. Thanks!</p>
  
  <p><strong>New Setup</strong> - Causing Error <code>Uninitialised Constant CartController</code></p>
  
  <p>Routes.rb</p>
  
  <pre><code> Checkout::Application.routes.draw do
  
     ActiveAdmin.routes(self)
  
     devise_for :admin_users, ActiveAdmin::Devise.config
  
     post '/add_to_cart/:product_id' =&gt; 'cart#add_to_cart', :as =&gt; 'add_to_cart'
  
     resources :carts
     resources :products
     resources :items
  
     root :to =&gt; 'products#index'
  
   end
  </code></pre>
  
  <p>Carts Controller</p>
  
  <pre><code> class CartsController &lt; ApplicationController
  
     def show
       @cart = current_cart
     end
  
     def add_to_cart
         current_cart.add_item(params[:product_id])
         redirect_to carts_path(current_cart.id)
     end
  
   end
  </code></pre>
  
  <p>Carts Model</p>
  
  <pre><code>class Cart &lt; ActiveRecord::Base
  has_many :items
  
  def add_item(product_id)
          item = items.where('product_id = ?', product_id).first
      if item
          # increase the quantity of product in cart
          item.quantity + 1
          save
      else
          # product does not exist in cart
          product = Product.find(product_id)
          items &lt;&lt; product
      end
      save
  end
  
  def total_price
      items.to_a.sum(&amp;:full_price)
  end
  end
  </code></pre>
  
  <p>Product#Index</p>
  
  <pre><code>&lt;table class="jobs"&gt;
      &lt;thead&gt;
          &lt;tr&gt;
              &lt;th scope="col" id="name"&gt;Product Code&lt;/th&gt;
              &lt;th scope="col" id="company"&gt;Name&lt;/th&gt;
              &lt;th scope="col" id="company"&gt;Price&lt;/th&gt;
              &lt;th scope="col" id="company"&gt;Action&lt;/th&gt;
          &lt;/tr&gt;
      &lt;/thead&gt;
  
      &lt;tbody&gt;
          &lt;% @product.each do |product| %&gt;
          &lt;tr&gt;    
              &lt;td&gt;&lt;%= product.product_code %&gt;&lt;/td&gt;
              &lt;td&gt;&lt;%= product.name %&gt;&lt;/td&gt;
              &lt;td&gt;&lt;%= gbp(product.price) %&gt;&lt;/td&gt;
              &lt;td&gt;&lt;%= button_to "Add to Cart", add_to_cart_path(:product_id =&gt; product), :method =&gt; :post %&gt;&lt;/td&gt;
          &lt;/tr&gt;
          &lt;% end %&gt;
      &lt;/tbody&gt;
  &lt;/table&gt;
  </code></pre>