Thomas
Thomas

Reputation: 723

NoMethodError in LineItemsController#create

I'm working with the Agile Web Development with Rails (4th edition) book and I bumped into a piece of code that is not working. I tried hard to figure out why it's not working, but I didn't make it.

BACKGROUD INFORMATION

The following classes are involved:

class Cart < ActiveRecord::Base
  has_many :line_items, :dependent => :destroy
end

class LineItem < ActiveRecord::Base
  belongs_to :product
  belongs_to :cart
end

class ApplicationController < ActionController::Base
  protect_from_forgery

  private

  def current_cart
    Cart.find(session[:cart_id])
  rescue ActiveRecord::RecordNotFound
    cart = Cart.create
    session[:cart_id] = cart.id
  end
end

NOW here is where it fails:

class LineItemsController < ApplicationController
  def index
    @line_items = LineItem.all`

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @line_items }
    end
  end

  # GET /line_items/1
  # GET /line_items/1.xml
  def show
    @line_item = LineItem.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @line_item }
    end
  end

  # GET /line_items/new
  # GET /line_items/new.xml
  def new
    @line_item = LineItem.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @line_item }
    end
  end

  # GET /line_items/1/edit
  def edit
    @line_item = LineItem.find(params[:id])
  end

  # POST /line_items
  # POST /line_items.xml
  def create
    @cart = current_cart
    product = Product.find(params[:product_id])
    @line_item = **@cart.line_items.build(:product => product)**`

ERROR MESSAGE *NoMethodError in LineItemsController#create undefined method `line_items' for 9:Fixnum Rails.root: /home/tmiskiew/depot*

*app/controllers/line_items_controller.rb:53:in `create' Request Parameters: {"product_id"=>"4", "authenticity_token"=>"dR4nL5zI+R7qIIPwNkl3EoaI1KyFWRokvh92m3PwD8o="}*

Anyone an idea what's wrong with @cart.line_items.build? I'm working rails 3.0.9 and ruby 1.8.7

Thanks Thomas

Upvotes: 2

Views: 2868

Answers (4)

Miguel Flores
Miguel Flores

Reputation: 13

I was having the same problem, and I felt really stuck on it. I fixed it by rollbacking the database and migrating it again.

Commands on terminal:

  • rake db:rollback
  • rake db:migrate

Upvotes: 0

Anthony
Anthony

Reputation: 91

I have similar error, it turns out I have a typo in Cart.rb:

class Cart < ActiveRecord::Base
    has_many :line_items, :dependent => :destroy
end

typo in :destory... and it cause the undefined method... hope this help.

Upvotes: 0

rnirnber
rnirnber

Reputation: 615

Here's how I fixed it...hope it works for you too.

/app/models/cart.rb:

def add_product(product_id)
    current_item = line_items.find_by_product_id(product_id)
    if current_item
      current_item.quantity += 1
    else
      current_item = line_items.build(:product_id => product_id)
    end
    return current_item
end

Add the return in. I'm not 100% sure why this was failing, but I think the function only returns the quantity, rather than a line item unless the if statement evaluates to true.

Upvotes: 2

cristian
cristian

Reputation: 8744

Have a look at your error
undefined method line_items' for 9:Fixnum

This says that @cart is a number, not an Cart object (@cart = current_cart from create action returns a number),
current_cart function returns a number because Cart.find(session[:cart_id]) returns recordNotFound and your rescue block from current_cart function will be executed.

Remember that every function in Ruby returns the result of the last executed line, so you will get returned the cart.id

Edit: For first comment, try to rewrite method

    def current_cart
      Cart.find(session[:cart_id])
      rescue ActiveRecord::RecordNotFound
        cart = Cart.create
        session[:cart_id] = cart.id
        cart # this will get returned
      end
    end

Upvotes: 2

Related Questions