This should be a fairly simple error to fix (I hope). When a joke
on my app is approved, I want that user
to be awarded 5 manpoints
(don't ask). I currently have this in my 'jokes_controller`:
def approve
@joke = Joke.find(params[:id])
@joke.update_attributes(approved: true)
if @joke.user.manpoints = nil
@joke.user.manpoints = 5
else
@joke.user.manpoints += 5
end
@joke.save
redirect_to jokes_path
end
I'm getting this error when I try to approve a joke:
undefined method `+' for nil:NilClass
I thought += was the "Ruby way" to do this? Can anyone set me straight?
Change =
in if
condition to ==
. I assume you need to compare manpoints
with nil
, not to assign nil
to manpoints. It should be like:
if @joke.user.manpoints == nil
...
You can omit ==
operator here:
unless @joke.user.manpoints
...
PS. Why are you excepting that manpoints
will be nil
?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.