I have the following code in an action:
@user = @current_user
@user.votes[1430]='up'
@user.update_attributes(params[:user])
Votes is a string type, there is no default value - I just want to set it for the first time when this action occurs.
Unfortunately I get this error: NoMethodError
You have a nil object when you didn't expect it! The error occurred while evaluating nil.votes
any ideas what I'm doing wrong?
The cause of the error seems to be that @user is a nil reference.
You can confirm this by using logging and checking in your console window:
logger.info "user is nil" if @user.nil?
You are assigning @user to be the value of @current_user. I've seen this pattern before and usually current_user is a function, declared elsewhere, not an instance variable. If you are using that pattern, the line should be something like @user = current_user instead.
(Additionally, if votes is a string, your second line appears to be referring to index 1430 of that string, which is probably not what you want either.)
I'm splitting off the comments as I don't want to hijack Evil Trouts response and we seem to be veering away from what was initially spoken about.
Unfortunately I get this error: NoMethodError
You have a nil object when you didn't expect it! The error occurred while evaluating nil.votes
For this, have a look at the authlogic example application. My guess is that you don't have the appropriate before_filter
set up and aren't requiring the user to be logged in on that action.
the 1430 is actually two seperate IDs
I don't fully follow this. It's a concatenation of two IDs? What are the IDs for? To be honest, I've never used an array as being a field type in my database so I don't know what the advantages of it are, but whenever I think to myself that an array would be a good idea, I usually question whether or not it wouldn't be better to just have it be a separate model, and hence, table.
It sounds like the situation you are describing might have a Question
which a User
can vote up on. If so, I might have a separate Voteable
model which would join the users with the questions they can vote 'up' on.
Maybe if you provide some more insight into this side of things, I can make a better suggestion. Cheers.
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.