【问题标题】:NoMethodError in LineItemsController#createLineItemsController#create 中的 NoMethodError
【发布时间】:2011-08-04 07:08:11
【问题描述】:

我正在使用 Rails 的敏捷 Web 开发(第 4 版)一书,但遇到了一段无法正常工作的代码。我努力想弄清楚为什么它不起作用,但我没有成功。

背景信息

涉及以下类:

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

现在 这是失败的地方:

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

错误信息 *LineItemsController 中的 NoMethodError#create 9:Fixnum 的未定义方法“line_items” Rails.root: /home/tmiskiew/depot*

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

有人知道@cart.line_items.build 有什么问题吗? 我正在使用 rails 3.0.9 和 ruby​​ 1.8.7

谢谢 托马斯

【问题讨论】:

    标签: ruby-on-rails-3


    【解决方案1】:

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

    这表示@cart 是一个数字,而不是 Cart 对象(创建操作中的@cart = current_cart 返回一个数字),
    current_cart 函数返回一个数字,因为 Cart.find(session[:cart_id]) 返回 recordNotFound 并且您的 rescue 块来自 current_cart 函数将被执行。

    请记住,Ruby 中的每个函数都会返回最后执行的行的结果,因此您将返回 cart.id

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

    def current_cart Cart.find(会话[:cart_id]) 救援 ActiveRecord::RecordNotFound 购物车 = Cart.create session[:cart_id] = cart.id cart # 这将被退回 结尾 结尾

    【讨论】:

    • 你的意思是我必须用返回会话完成 current_cart 的 else 分支吗?我试过了,但我现在得到 SQLite3::BusyException: database is locked: INSERT INTO "carts" ("updated_at", "created_at") VALUES ('2011-08-04 07:51:23.552125', '2011 -08-04 07:51:23.552125')
    • 返回会话而不是购物车,因为下面的代码是:`respond_to do |format| 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 end`
    • 您可以返回 cart 并可以使用您在 session[:cart_id] 中输入的内容,例如 @cart = current_cart 并调用 @cart.id,这将为您提供确切的值,例如 session[:cart_id] (毕竟你把会话cart.id)
    • 更改返回购物车或只是购物车后,我收到“无法重定向到零!”错误。 ActionController::ActionControllerError in LineItemsController#create
    • 请粘贴整个create 操作
    【解决方案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。

    【讨论】:

      【解决方案3】:

      我有类似的错误,原来我在 Cart.rb 中有错字:

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

      :destory... 中的错字,它会导致未定义的方法...希望对您有所帮助。

      【讨论】:

        【解决方案4】:

        我也遇到了同样的问题,我觉得自己真的被困住了。 我通过回滚数据库并再次迁移它来修复它。

        终端上的命令:

        • rake db:rollback
        • rake db:迁移

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-06-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-12-20
          • 1970-01-01
          相关资源
          最近更新 更多