small medium large xlarge

31 Dec 2012, 02:53
Oliver Damian (3 posts)

Please help. Any suggestions on how I can fix the error below. Thank you.

I’m getting this error:

NoMethodError in LineItemsController#create

undefined method `product=' for nil:NilClass
Rails.root: /home/oliverdamian/Rails.Training/depot

Application Trace | Framework Trace | Full Trace
app/controllers/line_items_controller.rb:46:in `create'

from this part of the line items controller:

# POST /line_items
  # POST /line_items.json
  def create
    @cart = current_cart
    product = Product.find(params[:product_id])
    @line_item = @cart.add_product(
    @line_item.product = product

    respond_to do |format|
        format.html { redirect_to @line_item.cart,
          notice: 'Line item was successfully created.' }
        format.json { render json: @line_item,
          status: :created, location: @line_item }
        format.html { render action: "new" }
        format.json { render json: @line_item.errors,
          status: :unprocessable_entity }
01 Jan 2013, 10:18
Pierre Sugar (56 posts)

I assume line 46 where the error raises is @line_item.product = product. If so look into app/models/line_item.rb whether it has the product attribute. Or could it be that @line_item is nil?

01 Jan 2013, 12:38
Oliver Damian (3 posts)

Thank you Pierre. I was able to fix that error by adding the following attributes in the LineItem model:

Class LineItem < ActiveRecord::Base
  attr_accessible :cart_id, :product_id, :quantity, :order_id, :product, :cart, :title
  belongs_to :product
  belongs_to :cart


But now I’m getting the error below on the show cart view. Any ideas on how to fix? Thanks again.

NoMethodError in Carts#show

Showing /home/oliverdamian/Rails.Training/depot/app/views/carts/show.html.erb where line #8 raised:

undefined method `title' for nil:NilClass
Extracted source (around line #8):

5: <h2>Your Pragmatic Cart</h2>
6: <ul>
7:   <% @cart.line_items.each do |item| %>
8:     <li><%= item.quantity %> &times; <%= item.product.title %></li>
9:   <% end %>
10: </ul>
Rails.root: /home/oliverdamian/Rails.Training/depot

Application Trace | Framework Trace | Full Trace
app/views/carts/show.html.erb:8:in `block in _app_views_carts_show_html_erb__3968409066690570960_69883151140560'
app/views/carts/show.html.erb:7:in `_app_views_carts_show_html_erb__3968409066690570960_69883151140560'
app/controllers/carts_controller.rb:18:in `show'
01 Jan 2013, 13:06
Pierre Sugar (56 posts)

Check in your app/models/product.rb if you have * has_many :line_items * attr_accessible .., :title, …

Or maybe you missed to create the title attribute when creating products?

01 Jan 2013, 13:51
Oliver Damian (3 posts)

The product model have those:

class Product < ActiveRecord::Base
  has_many :line_items

  before_destroy :ensure_not_referenced_by_any_line_item

  attr_accessible :description, :image_url, :price, :title
  validates :title, :description, :image_url, presence: true
  validates :price, numericality: {greater_than_or_equal_to: 0.01}
  validates :title, uniqueness: true
  validates :image_url, allow_blank: true, format: { with: %r{\.(gif|jpg|png)\Z}i,
            message: 'must be URL for GIF, JPG, or PNG image.'}


  # ensure that there are no line items referencing this product

  def ensure_not_referenced_by_any_line_item
    if line_items.empty?
      return true
      errors.add(:base, 'Line Items present')
      return false

And products have a title. For example:

Title: CoffeeScript

Description: <p> CoffeeScript is JavaScript done right. It provides all of JavaScript's functionality wrapped in a cleaner, more succinct syntax. In the first book on this exciting new language, CoffeeScript guru Trevor Burnham shows you how to hold onto all the power and flexibility of JavaScript while writing clearer, cleaner, and safer code. </p>

Image url: cs.jpg

Price: 36.0
04 Jan 2013, 08:55
Pierre Sugar (56 posts)

I am not sure but if you have declared attr_accessible :product but in your database you are only referencing to product_id than :product would not be accessible. That means that you cannot reference :product.

Change your attr_accessible to

attr_accessible :cart_id, :product_id

Than it should work

  You must be logged in to comment