简体   繁体   中英

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

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. If you don't know how to fix that, show us implementation of add_product .

It looks like your add_product method is returning an integer instead of the product you're expecting.

This means the @line_item.save is evaluating to <some number>.save , which is why you're getting the error.

Check add_product and make sure it returns the object instead of the id.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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