Rails 3 app throws "undefined method `update_attributes'" on save
In my controller a whole bunch of numbers are crunched. Percentages are multiplied by 100 so they can be stored as integers.
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
Then the values are supposed to be written to the user record as follows:
@user = User.where(id = curre开发者_开发问答nt_user.id)
@user.update_attributes(:strat_pct => strat_pct.to_i, :tact_pct => tact_pct.to_i, :exec_pct => exec_pct.to_i )
I am getting the following error message:
undefined method `update_attributes'
Thanks for your help.
Your issue is here:
@user = User.where(id = current_user.id)
In the above, you are doing an assignment to the variable id to the value of current_user.id which is not your intention in this case.
If you do:
p @user.class
Then it will return nilClass.
You need either:
@user = User.find(current_user.id)
Or:
@user = User.where(:id => current_user.id)
Although based on your code, current_user is probably an instance of User. I would say you don't need to make any additional calls, you should be able to do:
current_user.update_attributes(:strat_pct => strat_pct.to_i, :tact_pct => tact_pct.to_i, :exec_pct => exec_pct.to_i )
精彩评论