Ruby on rails OrdersController中的NameError#创建未定义的局部变量或方法'line#u items';

Ruby on rails OrdersController中的NameError#创建未定义的局部变量或方法'line#u items';,ruby-on-rails,e-commerce,shopping-cart,activemerchant,Ruby On Rails,E Commerce,Shopping Cart,Activemerchant,我正在尝试创建一个与活动商户结帐。我正在使用Ryan Bates活动商户教程/和《敏捷web开发》一书 我得到以下错误,我不知道如何修复它来创建一个成功的订单。非常感谢您的帮助 NameError in OrdersController#create undefined local variable or method `line_items' for #<Order:0x007fcc467134b8> Rails.root: /Users/macuser/rails_proje

我正在尝试创建一个与活动商户结帐。我正在使用Ryan Bates活动商户教程/和《敏捷web开发》一书

我得到以下错误,我不知道如何修复它来创建一个成功的订单。非常感谢您的帮助

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'

这一行在您的
订单中注释掉了
型号:

# has_many :line_items, :dependent => :destroy
由于您在
从购物车添加商品
方法中使用此关联,因此如果您将其注释掉,则此关联无效。

错误源于:

 line_items << item
行项目
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
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
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
# has_many :line_items, :dependent => :destroy
 line_items << item