简体   繁体   English

Ruby on Rails控制器类中的错误

[英]error in ruby on rails controller class

I am getting this sort of error, earlier it wored properly but some how it is the error. 我遇到了这种错误,早先它可以正常解决,但是有些错误是怎么回事。

      undefined method `save' for 2:Fixnum

And this my code in line_item_controller.rb/create 这是我在line_item_controller.rb / create中的代码

def create
  @cart = current_cart
  product = Product.find(params[:product_id])
  @line_item = @cart.add_product(product.id)

  respond_to do |format|
    if @line_item.save
      format.html { redirect_to store_url}
      format.js   { @current_item = @line_item }
      format.json { render :json => @line_item, :status => :created, :location => @line_item }
    else
      format.html { render :action => "new" }
      format.json { render :json => @line_item.errors, :status => :unprocessable_entity }
    end
  end
end

please help! 请帮忙!

@cart.add_product seems return a number ( Fixnum ) instead of a model object, as you expect. @cart.add_product似乎返回数字( Fixnum )而不是您期望的模型对象。 If you don't know how to fix that, show us implementation of add_product . 如果您不知道如何解决该问题,请向我们展示add_product实现。

It looks like your add_product method is returning an integer instead of the product you're expecting. 看来您的add_product方法正在返回一个整数,而不是您期望的乘积。

This means the @line_item.save is evaluating to <some number>.save , which is why you're getting the error. 这意味着@line_item.save评估为<some number>.save ,这就是为什么会出现错误的原因。

Check add_product and make sure it returns the object instead of the id. 检查add_product并确保它返回对象而不是id。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM