如何在 Devise 中仅使用电子邮件添加用户
我希望我的用户能够仅通过提供电子邮件地址来“订阅”,而不强迫他们使用密码注册,除非他们愿意。
我可以添加我自己的验证设置来允许这样做,但我怀疑如果我开始添加没有密码的用户或使用我自己的“非注册”字段标记他们,它会干扰 Devise 的其余部分的工作方式。
目前,最简单的解决方案似乎是将这些用户放在不同的表中,但这有点奇怪。有什么想法吗?
I would like my users to be able to 'subscribe' by only providing an email address without forcing them to sign-up with a password unless they want to.
I can add my own validation settings to allow this but i suspect it will interfere with the way the rest of Devise works if i start adding users without passwords or flagging them with my own 'non-registered' field.
At the moment the easiest solution seems to put these users in a different table but that has a bit of a smell about it. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我有一个执行类似操作的应用程序 - 我们专门使用 Open ID 和 OAuth 进行登录,虽然我们存储用户的电子邮件地址(Google 帐户或 Facebook 在身份验证期间将其发回),但他们从不使用它登录,因此没有/需要密码。
这是用户通过 Facebook 注册时发生的情况的回调:
Devise.Friendly_token[...]
只是为用户生成一个随机的 20 个字符的密码,我们正在使用默认的用户模型用设计就好了。我的方法就是以类似的方式为您的用户设置密码,因为您永远不会使用他们的密码。另外,如果您改变主意并希望他们能够登录,您只需更改数据库中的用户密码并构建登录表单,devise 将处理其余的事情。I have an app that does something similar - we use Open ID and OAuth exclusively for logins, and while we store the user's email address (Google Account or Facebook sends it back during authentication) they never login using it and so don't have/need a password.
This is a callback for what happens when a user signs up via Facebook:
The
Devise.friendly_token[...]
just generates a random 20 character password for the user, and we are using our default User model with devise just fine. Just setting the password in a similar way on your users would be my approach, since you are never going to use their password. Also, if you ever change your mind and want them to be able to login, you can just change a user's password in the database and build a login form, and devise will take care of the rest.布雷特回答的另一个选择是覆盖password_required?用户模型上的方法。
如果您存储多个omniauth 提供程序,那么类似的操作也应该有效。
此解决方案完全归功于 Ryan Bates 在 Devise 和 Omniauth 上的railscast:http:// railscasts.com/episodes/235-devise-and-omniauth-revised
Another option further to Brett's answer is to override the password_required? method on the User model.
If you store more than one omniauth provider then something like this should also work.
Full credit to Ryan Bates' railscast on Devise and Omniauth for this solution: http://railscasts.com/episodes/235-devise-and-omniauth-revised