我目前对这个很困惑。我最近不得不更新我的 rails 应用程序,现在每当我尝试保存一个也尝试保存访问者对象的对象时都会看到这个错误弹出窗口。这是失败的代码:
if @sale.shift_id.nil?
@sale.update_attribute(:shift_id,session[:shift_id])
@title = "New Sale"
@sale = Sale.new
@products = Product.order("name")
@shifts = Shift.order("starttime")
#this line below is line 51, which the output says is the failing line.
Visitor.new(:gender => 'Other', :shift_id => session[:current_shift_id], :reason_id => Reason.find_by_name("Products")).save
redirect_to(newsale_path, :notice => 'successfully added the sale')
else
我得到的错误是:
undefined method `to_i' for #<Reason:0x56f5848>
Rails.root:
Application Trace | Framework Trace | Full Trace
app/controllers/sales_controller.rb:51:in `new'
app/controllers/sales_controller.rb:51:in `create'
起初我认为它可能是 Reason.find_by_name,所以我用一个有效的整数值替换它,它仍然失败。我能看到的唯一可能失败的其他代码是 routes.rb,它在下面有一个条目:
match 'newsale', :to => 'sales#newsale'
最后,webrick 的输出令人困惑,因为它实际上显示了保存 @sale 对象的 sql 调用,然后我得到以下几行:
SELECT "reasons".* FROM "reasons" WHERE "reasons"."name" = 'Products' LIMIT 1
Completed 500 Internal Server Error in 104ms