Automatically set password for user - Devise

南笙酒味 提交于 2019-12-07 13:17:28

问题


Following this tutorial, I setup a simple registration system with devise, where a user enters their email address, they are sent a confirmation email, they click on it, it takes them to a form where they have to enter their password and complete the registration.

How do I skip that password form so that once the user clicks on the confirmation link in their email, their account is created? How can I automatically set simple password for all users?


回答1:


Create a method in your user model to use it instead of the create method. Let's call it create_with_password

def self.create_with_password(attr={})
   generated_password = attr[:first_name] + "123"
   self.create(attr.merge(password: generated_password, password_confirmation: generated_password))
end

Override devise's registration controller to use your new method which generates the two necessary fields for devise password and password_confirmation using the first_name attribute in your user model for example then 123 like in John123

In your controller after your parameters are sanitized. You should call User.create_with_password(user_params)



来源:https://stackoverflow.com/questions/28398803/automatically-set-password-for-user-devise

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!