Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8533471
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T09:58:20+00:00 2026-06-11T09:58:20+00:00

I am trying to create a checkout with active merchant. I am using Ryan

  • 0

I am trying to create a checkout with active merchant. I am using Ryan Bates Active merchant tutorial/ and the Agile web development book.

I get the following error, I’m not sure how to fix it to create a successful order. Any help is much appreciated.

NameError in OrdersController#create 

undefined local variable or method `line_items' for #<Order:0x007fcc467134b8>
Rails.root: /Users/macuser/rails_projects/listpro-a

Application Trace | Framework Trace | Full Trace
app/models/order.rb:33:in `block in add_line_items_from_cart'
app/models/order.rb:31:in `add_line_items_from_cart'
app/controllers/orders_controller.rb:33:in `create'

OrdersController

class OrdersController < ApplicationController

def index
   @orders = Order.paginate :page=>params[:page], :order=>'created_at desc',
   :per_page => 10
   respond_to do |format|
     format.html # index.html.erb
     format.xml { render :xml => @orders }
  end
end

def new
@cart = current_cart
@order_total = current_cart.total_price

if @cart.line_items.empty?
  redirect_to services_path, :notice => "Your cart is empty"
  return
end

@order = Order.new

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

def create
@order = current_cart.build_order(params[:order])
@order.ip_address = request.remote_ip
@order.add_line_items_from_cart(current_cart)
respond_to do |format|
  if @order.save
    if @order.purchase          
      Cart.destroy(session[:cart_id])
      session[:cart_id] = nil
      format.html { redirect_to(redirect_to new_listing_path, :notice => 'Thank you for your order.') }
      format.xml { render :xml => @order, :status => :created, :location => @order }
    else
      render :action => "failure"
    end

  else
    format.html { render :action => "new" }
    format.xml { render :xml => @order.errors,
    :status => :unprocessable_entity }
  end
end
end  
end

Order Model

class Order < ActiveRecord::Base
  # PAYMENT_TYPES = [ "visa", "master card", "Amex", "Discover" ] Controll the payment     options via Model
  attr_accessible :card_expires_on, :card_type, :first_name, :ip_address, :last_name,    :cart_id, :card_number, :card_verification



 # belongs_to :user
 # belongs_to :house
   belongs_to :cart

 has_many :transactions, :class_name => "OrderTransaction"
 # has_many :line_items, :dependent => :destroy

 attr_accessor :card_number, :card_verification

 validate :validate_card, :on => :create

 def purchase
    response = GATEWAY.purchase(price_in_cents, credit_card)
    transactions.create!(:action => "purchase", :amount => price_in_cents, :response => response)
    # cart.update_attribute(:purchased_at, Time.now) if response.success?
    response.success?
  end

  # Issue is here
  def price_in_cents 
  (cart.total_price*100).round
  end

  def add_line_items_from_cart(cart)
     cart.line_items.each do |item|
     item.cart_id = nil
     line_items << item
  end
 end


  private

  def validate_card
    unless credit_card.valid?
      credit_card.errors.full_messages.each do |message|
        errors[:base] << message
      end
    end
   end

  def credit_card
    @credit_card ||= ActiveMerchant::Billing::CreditCard.new(
     :type               => card_type,
      :number             => card_number,
      :verification_value => card_verification,
      :month              => card_expires_on.month,
      :year               => card_expires_on.year,
      :first_name         => first_name,
      :last_name          => last_name

     )
   end
 end

Carts Model

class Cart < ActiveRecord::Base
  attr_accessible :purchased_at, :house_id, :user_id
  has_many :line_items # , :dependent => :destroy
  has_one :order
  # has_many :services, :through => :line_items
  # belongs_to :user


  def total_price
    # convert to array so it doesn't try to do sum on database directly
    line_items.to_a.sum { |line_item| line_item.total_price }
  end

  def add_service(service_id)
   current_item = line_items.find_by_service_id(service_id)

   if current_item

          # render current cart with flash " you already have this service"
       else 
         current_item = line_items.build(:service_id => service_id)
       end
     current_item
   end
end

Applications Controller

class ApplicationController < ActionController::Base
  protect_from_forgery

  rescue_from CanCan::AccessDenied do |exception|
    redirect_to root_path, :alert => exception.message
  end


  helper :all


  private

  def current_cart

    Cart.find(session[:cart_id])
    rescue ActiveRecord::RecordNotFound
    cart = Cart.create(:user_id => current_user.id)
    session[:cart_id] = cart.id
    cart
  end

DB SCHEMA

create_table "carts", :force => true do |t|
    t.integer  "user_id"
    t.datetime "purchased_at"
    t.datetime "created_at",   :null => false
    t.datetime "updated_at",   :null => false
  end

create_table "order_transactions", :force => true do |t|
    t.integer  "order_id"
    t.string   "action"
    t.integer  "amount"
    t.boolean  "success"
    t.string   "authorization"
    t.string   "message"
    t.text     "params"
    t.datetime "created_at",    :null => false
    t.datetime "updated_at",    :null => false
  end


create_table "orders", :force => true do |t|
    t.integer  "cart_id"
    t.string   "ip_address"
    t.string   "first_name"
    t.string   "last_name"
    t.string   "card_type"
    t.integer  "house_id"
    t.integer  "user_id"
    t.date     "card_expires_on"
    t.datetime "created_at",      :null => false
    t.datetime "updated_at",      :null => false
  end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T09:58:21+00:00Added an answer on June 11, 2026 at 9:58 am

    This line is commented out in your Order model:

    # has_many :line_items, :dependent => :destroy
    

    Since you are using this association in the add_line_items_from_cart method, it doesn’t work if you comment it out.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create undo checkout recursively..., context menu with arguments (I'm using clearmenuadmin.exe
I'm trying create a bot which automatically likes Facebook posts. Using Mechanize I can
Ok so I am trying create a login script, here I am using PHP5
Trying to create a new Dedicated Cache Role in Windows Azure but get the
Hi I am using aptana studio 3. When i am trying to checkout the
I am trying to create a one step checkout, but in the checkout page
I'm trying to create a new entry in subversion for development. This is based
I get this very nondescript error trying to create my next two tables, I
I'm trying to create a checkout page in PHP. The page that lists the
I'm trying to create different checkout sessions for the logged in customer at a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.