自动为用户设置密码-设计 [英] Automatically set password for user - Devise

查看:136
本文介绍了自动为用户设置密码-设计的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

遵循本教程,我通过设计设置一个简单的注册系统,在该系统中,用户输入电子邮件地址,将向他们发送确认电子邮件,然后单击该电子邮件,将其带到一个表单,在该表单中,他们必须输入密码并完成注册.

如何跳过该密码表,以便一旦用户单击电子邮件中的确认链接,便创建了他们的帐户?如何为所有用户自动设置简单密码?

解决方案

在用户模型中创建一个方法来使用它,而不是create方法.我们称之为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

重写devise的注册控制器,以使用您的新方法,该方法会使用用户模型中的first_name属性(例如,像John123中的123)生成用于设计passwordpassword_confirmation的两个必要字段. >

在清除参数后,在控制器中.您应该致电User.create_with_password(user_params)

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?

解决方案

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)

这篇关于自动为用户设置密码-设计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆