问题描述
||
在我的控制器中,一大堆数字被弄乱了。将百分比乘以100,以便可以将它们存储为整数。
tot = @mot[1] + @mot[2] + @mot[3]
exec_pct = @mot[3] / tot * 100
tact_pct = @mot[2] / tot * 100
strat_pct = @mot[1] / tot * 100
然后,应将这些值写入用户记录,如下所示:
@user = User.where(id = current_user.id)
@user.update_attributes(:strat_pct => strat_pct.to_i,:tact_pct => tact_pct.to_i,:exec_pct => exec_pct.to_i )
我收到以下错误消息:
undefined method `update_attributes\'
谢谢你的帮助。
解决方法
您的问题在这里:
@user = User.where(id = current_user.id)
在上面,您正在将变量id赋给current_user.id的值,在这种情况下,这并不是您的意图。
如果您这样做:
p @user.class
然后它将返回nilClass。
您需要:
@user = User.find(current_user.id)
要么:
@user = User.where(:id => current_user.id)
尽管根据您的代码,current_user可能是User的实例。我想说您不需要拨打任何其他电话,您应该可以:
current_user.update_attributes(:strat_pct => strat_pct.to_i,:tact_pct => tact_pct.to_i,:exec_pct => exec_pct.to_i )