Only One Product Variant added to Cart, and It's always the first one added to my db

144 Views Asked by At

I'm setting up a simple online store with no user system - just a session following a cart. Currently, I am able to select a Product (could be thought of as a product category), and within that product page select a ProductVariant (product_variant_id) belonging to it.

The problem is that when I add different product_variants, only the first one that is saved to the db is ever added to the cart. I can select the product_variants in the dropdown, but again only the first one added to the db in my record is added to the cart as an order_item.

My relevant Models:

Product

  has_many :product_variants, dependent: :destroy

  has_many :order_items, :through => :product_variants

ProductVariant

  belongs_to :product
  has_many :order_items, dependent: :destroy

OrderItem

  belongs_to :order, optional: true
  belongs_to :cart, optional: true
  belongs_to :product_variant
  belongs_to :product

Cart 

has_many :order_items

And here is my product show page in show.html.erb with the option to select a product_variant

<%= link_to products_path do %>
<h4>Back to store gallery</h4>
<% end %>

<section class="flexbox">
  <div class="flex">
      <%= image_tag @product.image_1.show.url %>
  </div>
  <div class="flex">
      <h2><%= @product.title %></h2>

      <div class="product-description">
        <h5>Description:</h5>
          <p><%= @product.description %></p>
      </div>
  </div>
</section>


<%= simple_form_for [@product, @product_variant, @order_item] do |f| %>
  <%= f.input :quantity %>

  <%= f.button :submit, "Add to cart" %>
<% end %>



Product selection: <br>
<select name="product[product_variant_id]">
    <% @product.product_variants.each do |product_variant| %>
        <option value="<%= product_variant.id %>"><%= product_variant.item %>/<%= product_variant.size %>/<%= product_variant.color %>/<%= number_to_currency product_variant.price_in_dollars %></option>
    <% end %>
</select>

Lastly, here is my order_items controller

class OrderItemsController < ApplicationController
  
  def create
    @product = Product.friendly.find(params[:product_id])
    # find the product_variant
    @product_variant = ProductVariant.find(params[:product_variant_id])
    # quantity? - comes from the form data
    @quantity = form_params[:quantity]
    @current_cart.order_items.create(product: @product, product_variant: @product_variant, quantity: @quantity)
    flash[:success] = "Thanks for adding to your cart"
    redirect_to product_path(@product)
  end
  
  def update
    @product = Product.friendly.find(params[:product_id])
    @product_variant = ProductVariant.find(params[:product_variant_id])
    @order_item = OrderItem.find(params[:id])
    @order_item.update(form_params)
    flash[:success] = "Thanks for updating your cart"
    redirect_to cart_path
  end
  
  def destroy
    @product = Product.friendly.find(params[:product_id])
    @product_variant = ProductVariant.find(params[:product_variant_id])
    @order_item = OrderItem.find(params[:id])
    @order_item.delete
    flash[:success] = "Product removed from cart"
    redirect_to cart_path
  end
  
  def form_params
    params.require(:order_item).permit(:quantity)
  end
end

Thanks for any insight into what might be going wrong here, and please don't hesitate if you need me to provide any more relevant code, would be happy to.

Aaron

0

There are 0 best solutions below