表单传递String而不是Float

时间:2011-10-25 11:41:55

标签: ruby-on-rails-3 model-view-controller view methods

我在模型上有一个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),只是移动到视图并且有效

1 个答案:

答案 0 :(得分:0)

似乎为imc赋值可以定义局部变量,而不是为对象的属性赋值。

只需将imc = value1/(value2*value2)替换为self.imc = value1/(value2*value2)

def calc_imc(value1,value2)
  self.imc = value1/(value2*value2)
end