vendredi 11 novembre 2016

Rails pass has many thought to a action

On has many thought select option

  1. product has many colors thought => colorship
  2. color has many products thought => colorship
def show
  Product.find(params[:id])
  @colors_for_dropdown = @product.colors.collect { |co| [co.name, co.id] }
end

<%= select_tag :color_id, options_for_select(@colors_for_dropdown.unshift(['Select Color', nil])) %>

I tried to add the color of product that the user choose on the cart. but i'm getting error, someone have ant tip or idea what is wrong?

def add
  product = Product.find(params[:id])



 if product
    current_user.cart = Cart.new 

    color = product.colorship.first.color.name 

     current_user.cart.orders.find_by_product_id_and_color_id(product.id, nil, 
color.nil? ? nil : color.id)
          end
        end

Aucun commentaire:

Enregistrer un commentaire