问题
I recently created a way to change the current user password, however after saving the record, my UserSession.find returns nil, I tried by writing UserSession.new({...}).save with no luck, any suggestion on how to resolve this issue?
Here is my code, notice that is run through an AJAX request (this is a method under UserSessionController):
def
change_my_password
#print "--------------RECORD: #{current_user_session.record.as_json}-------- #{current_user_session.user.as_json}"
user = current_user
user_email = user.email
user_remember_me = user.remember_created_at
response = {
:success => false,
:message_code => Extjs::MessageCodes::ERROR,
:message => 'Si è verificato un errore',
:total => 0,
:root => []
}
if user.valid_password?(params[:old_password], true)
user.password = params[:new_password]
user.password_confirmation = params[:confirm_password]
response[:message] = 'La nuova password e la conferma non coincidono o sono troppo brevi'
if user.save
response[:success] = true
response[:message_code] = Extjs::MessageCodes::SUCCESS
response[:message] = 'Password modificata con successo'
end
else
response[:message] = 'La password precedente non coincide con quella attualmente in uso'
end
respond_to do |format|
format.extjson { render :json => response }
end
end
回答1:
Ensure that you haven't set the maintain_sessions parameter to false, i.e.:
acts_as_authentic do |c|
c.maintain_sessions = false # change this to true.
end
OR
Update the session manually after save:
user.send(:update_sessions)
OR
Recreate the session after the save:
UserSession.create(user)
回答2:
Actually I didn't find a solution to this issue: I solved by forcing a login again after changing password (whcih is even added security, so not that bad).
I think KandadaBoggu is right with his answer, but I tested all of them and they are not working for me.
Maybe it's an issue created by something wrong in my code, I really don't know. At the moment I just consider this as the solution because it's actually working for my software.
If a better answer is found, I would be happy to mark it.
Thanks to everyone.
来源:https://stackoverflow.com/questions/11143656/usersession-find-returns-nil-after-changing-password