表单传递 String 而不是 Float
我在模型上有一个 before_save 方法,它使用 2 个属性进行一些计算,当我提交表单时,出现错误:
TypeError in AntropometricasController#create
can't convert String into Integer
我正在使用 ruby 1.9.2 和 Rails 3.0.9
编辑:通过删除 before_save 并运行解决了这个问题创建操作的方法(不知道它是否正确):
@evaluation.calc_imc(@evaluation.value1.to_f,@evaluation.value2.to_f)
在我的模型上,方法是:
attr_accessor :imc
def calc_imc(value1,value2)
imc = value1/(value2*value2)
end
但是我的 evaluation.imc
为零。我想在视图上显示这个值,我做错了什么?
更新:解决了我的问题,我正在使用@evaluation.calc_imc(@evaluation.value1.to_f,@evaluation.value2.to_f)
,刚刚移至视图,它工作过
I have a before_save method on a model that make some calculations with 2 attributes, when I submmit my form I got the error:
TypeError in AntropometricasController#create
can't convert String into Integer
I'm using ruby 1.9.2 and rails 3.0.9
EDIT: Solved this problem by removing the before_save and running the method on the create action (dont know if its correct):
@evaluation.calc_imc(@evaluation.value1.to_f,@evaluation.value2.to_f)
On my model the method is:
attr_accessor :imc
def calc_imc(value1,value2)
imc = value1/(value2*value2)
end
But my evaluation.imc
is nil. I want to display this value on a view, what am I doing wrong?
UPDATE: Solved my problem, I was using @evaluation.calc_imc(@evaluation.value1.to_f,@evaluation.value2.to_f)
, just moved to the view and it worked
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看来给imc赋值可以定义局部变量,而不是给对象的属性赋值。
只需将
imc = value1/(value2*value2)
替换为self.imc = value1/(value2*value2)
:It seems that assignment of value to imc can define local variable instead of assigning value to the object`s property.
Just replace
imc = value1/(value2*value2)
withself.imc = value1/(value2*value2)
: