2016-07-24 59 views
-2
def create 
user_password = params[:session][:password] 
user_email = params[:session][:email] 
user = user_email.present? && User.find_by(email: user_email) 

if user and user.valid_password? user_password 
    sign_in user 
    user.generate_authentication_token! 
    user.save 
    render json: user, status: 200, location: [:api, user] 

else 
    #render json: { errors: "Invalid email or passwords"}, status: 422 
    render json: { errors: "Invalid email or passwords"}, status: 422 

end 

user.valid_password色器件宝石不起作用

无法登录用户登录后,似乎valid_password?方法不会加密与设备存储在数据库中的加密密码相同格式的密码,任何帮助将不胜感激。

回答

2

该代码适用于创建会话。请检查您是否正确创建用户!

为此在你的Rails控制台

User.create({email:"[email protected]",password:"anything"}) 
User.find(type the id of new user).valid_password?("anything") 

如果返回true,那么你没有正确地创建用户。检查你创建用户的方法。

+0

感谢您的帮助:) –