我最近创建了一种更改当前用户密码的方法,但是在保存记录后,我的 UserSession.find 返回 nil,我尝试通过编写 UserSession.new({...}).save 没有运气,任何关于如何解决这个问题?
这是我的代码,请注意通过 AJAX 请求运行(这是 UserSessionController 下的方法):
定义
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