¿Por qué recibo nil can't be coerced into BigDecimal
cuando trato de realizar un cálculo: Aquí está el código:Ruby on Rails nula no puede ser obligado a BigDecimal
/drink.rb
class Drink < ActiveRecord::Base
belongs_to :menu
before_save :total_amount
def total_amount
self.total_amount = self.price * self.quantity
end
modelo/menu.rb
class Menu < ActiveRecord::Base
has_many :drinks, :dependent => :destroy
accepts_nested_attributes_for :drinks, :allow_destroy => true
#Validations
end
* bebida es el (anidada) modelo del niño y menú de la p enviaban modelo Cuando intento crear una nueva bebida de la pantalla del navegador siguiente mensaje de error nil can't be coerced into BigDecimal app/models/drink.rb:7:in 'total-amount' app/controllers/menus_controller.rb:47:in 'create' app/controllers/menus_controller.rb:46:in 'create'
app/db/migración
class CreateDrinks < ActiveRecord::Migration
def change
create_table :drinks do |t|
t.string :name
t.decimal :quantity,:precision => 8, :scale => 2
t.decimal :price, :precision => 8, :scale => 2
t.decimal :vat, :precision => 8, :scale => 2
t.references :menu
t.timestamps
end
add_index :drinks, :menu_id
end
end
controladores/drinks_controller.rb
class DrinksController < ApplicationController
# GET /drinks
# GET /drinks.json
def index
@drinks = Drink.all
respond_to do |format|
format.html # index.html.erb
format.json { render :json => @drinks }
end
end
# GET /drinks/1
# GET /drinks/1.json
def show
@drink = Drink.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render :json => @drink }
end
end
# GET /drinks/new
# GET /drinks/new.json
def new
@drink = Drink.new
respond_to do |format|
format.html # new.html.erb
format.json { render :json => @drink }
end
end
# GET /drinks/1/edit
def edit
@drink = Drink.find(params[:id])
end
# POST /drinks
# POST /drinks.json
def create
@article = Drink.new(params[:drink])
respond_to do |format|
if @drink.save
format.html { redirect_to @drink, :notice => 'Drink was successfully created.' }
format.json { render :json => @drink, :status => :created, :location => @article }
else
format.html { render :action => "new" }
format.json { render :json => @drink.errors, :status => :unprocessable_entity }
end
end
end
# PUT /drinks/1
# PUT /drinks/1.json
def update
@drink = Drink.find(params[:id])
respond_to do |format|
if @drink.update_attributes(params[:drink])
format.html { redirect_to @drink, :notice => 'Drink was successfully updated.' }
format.json { head :ok }
else
format.html { render :action => "edit" }
format.json { render :json => @drink.errors, :status => :unprocessable_entity }
end
end
end
# DELETE /drinks/1
# DELETE /drinks/1.json
def destroy
@drink = Drink.find(params[:id])
@drink.destroy
respond_to do |format|
format.html { redirect_to drinks_url }
format.json { head :ok }
end
end
end
Por favor, puede ¿alguien me dice qué le pasa al código?
¿Qué valores obtienes por 'price' y' quantity' en 'total_amount'? Haga un 'Logger.debug" # {self.price} # {self.quantity} "' como la primera línea en 'total_amount'. –
esto es lo que obtengo con 'Logger.debug ...' como la primera línea en el método total_amount' método no definido 'debug' para Logger: Class' – blawzoo