2011-08-04 35 views
2

我正在使用Agile Web Development with Rails(第4版)一书,并且遇到了一段不起作用的代码。我努力弄清楚它为什么不起作用,但我没有做到。LineItemsController中的NoMethodError#create

BACKGROUD信息

下列类别涉及:

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 这里就是失败:

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)**` 

错误信息 * NoMethodError在LineItemsController#创建 未定义的方法OD`line_items' 9:Fixnum对象 Rails.root:/家庭/ tmiskiew /仓库*

*应用程序/控制器/ line_items_controller.rb:53:在`创建” 请求 参数: { “PRODUCT_ID”= > “4”, “authenticity_token”=> “dR4nL5zI + R7qIIPwNkl3EoaI1KyFWRokvh92m3PwD8o =”} *

任何一个想法有什么不对@ cart.line_items.build? 我的工作轨道3.0.9和1.8.7的红宝石

感谢 托马斯

回答

2

看一看你的错误
undefined method line_items' for 9:Fixnum

这是说@cart是一个数字,而不是一个车对象(从创建动作@cart = current_cart返回一个数),
current_cart函数返回一个数,因为 Cart.find(会话[:cart_id])返回recordNotFound并从你的块函数将被执行。

请记住,在Ruby中每个函数返回最后执行的行的结果,所以你会得到返回的cart.id

编辑:对于第一个评论,尝试重写方法

 
    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 
+0

你的意思是我必须完成current_cart的其他分支返回会议?我试过了,但现在我正在获取SQLite3 :: BusyException:数据库被锁定:INSERT INTO“carts”(“updated_at”,“created_at”)VALUES('2011-08-04 07:51:23.552125','2011 -08-04 07:51:23.552125' ) – Thomas

+0

返回会议,而不是车,是因为下面的代码是: '的respond_to做|格式| if line_item.save format.html {redirect_to(@ line_item.cart, :notice =>'订单项成功创建')} format.xml {render:xml => @line_item, :status = >:created,:location => @line_item} else format.html {render:action =>“new”} format.xml {render:xml => @ line_item.errors, :status =>:unprocessable_entity} 结束 end' – Thomas

+0

您可以返回'cart',并且可以使用你把会话[:cart_id]这样的'@cart = current_cart'和调用'@ cart.id',这会给你会喜欢的精确值[:cart_id](毕竟你放在会话cart.id) – cristian

2

这里的我如何修复它...希望它也适用于您。

/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 

添加回在我不是100%肯定这是为什么失败了,但我认为功能只返回的数量,而。除非if语句的计算结果为true,否则不会生成订单项。

0

我有类似的错误,事实证明我有Cart.rb一个错字:

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

错字在:destory ...它会导致未定义的方法......希望这帮助。

0

我有同样的问题,我觉得真的很憋屈了。 我通过回滚数据库并再次迁移它来修复它。

命令在终端:

  • 耙分贝:回滚
  • 耙分贝:迁移
相关问题