samedi 27 février 2016

nil can't be coerced into Float,help please

I am having one problem. Hope someone can help..I have this error

"Action controller Error is : /app/views/carts/show.html.erb where line #6 raised:

" nil can't be coerced into Float "

Here are the code file

Cart Item Model

class CartItem


attr_reader :product_id, :quantity



def  initialize (product_id, quantity= 1)
    @product_id = product_id
    @quantity = quantity
  end

  def increment
     @quantity = @quantity + 1
  end

  def product
    Product.find product_id
  end

  def total_price
    product.price * quantity
  end
end

Show View

<% @cart.items.each do |item|%>
<table class="table table-hover">
  <tr>
    <td><%= item.quantity %></td>
    <td><%= item.product.name %></td>
    <td><%= item.total_price %></td>
  </tr>
</table>

<% end %>

Cart Controller

class CartsController < ApplicationController
  before_filter :initialize_cart

  def add
    @cart.add_item params[:id]
    session["cart"] = @cart.serialize
    product = Product.find params[:id]
    redirect_to :back, notice:  "Added #{product.name} to cart."
  end

  def show

  end

end

Thank you in advance

Aucun commentaire:

Enregistrer un commentaire