如何为 Ruby on Rails 配置 Devise 以将电子邮件和密码存储在用户模型以外的其他位置? [英] How can I configure Devise for Ruby on Rails to store the emails and passwords somewhere other than in the user model?

查看:13
本文介绍了如何为 Ruby on Rails 配置 Devise 以将电子邮件和密码存储在用户模型以外的其他位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将电子邮件存储在单独的表中,并允许用户保存多封电子邮件并使用其中任何一封登录.我还想将密码存储在不同的表中.

I'd like to store emails in a separate table and allow users to save multiple emails and log in with any of them. I'd also like to store passwords in a different table.

如何配置 Devise 以将身份验证信息存储在其他地方?

How can I configure Devise to store authentication info elsewhere?

最坏的情况,如果我只需要侵入它,是否有一个生成器可以将所有内容移植到应用程序中?我注意到有一个视图生成器.

Worst case scenario, if I just have to hack into it, is there a generator to just port everything over to the app? I noticed there was a generator for the views.

推荐答案

为此,您只需正确设置模型,创建电子邮件关联.此外,您必须覆盖设计中的用户查找器方法,为此您必须覆盖 self.find_for_database_authentication(conditions) 方法.

To do this, you have just to set up your models properly, create your email association. Also you have to override the user finder method from devise, to do this you have to override self.find_for_database_authentication(conditions) method.

class User < ActiveRecord::Base
  has_many :emails
  accepts_nested_attributes_for :emails
  ...
  def self.find_for_database_authentication(conditions)
    email = conditions.first
    self.where(:emails=>{:name=>email})
  end
end

此外,您还必须将电子邮件添加到您的 devise.rb 中才能使用它.

Also you have to add the emails to your devise.rb to use it.

Devise.setup do |config|
  ...
  config.authentication_keys = [ :emails ]
  ...
end

更新

第一个问题:是的,设计中的方法将被自动覆盖.

First question: Yes, the method from the devise will bee overrided automatically.

第二个问题:你应该有 your_application_path/config/initializers/devise.rb我认为它是在您使用设计 gem 时创建的.如果你没有,你可以创建它.我指定的内容不错,省略点(...)

Second question: You should have your_application_path/config/initializers/devise.rb I think it's created when you use the devise gem. If you don't have you can create it. The content I specified is good, just omit the dots (...)

这篇关于如何为 Ruby on Rails 配置 Devise 以将电子邮件和密码存储在用户模型以外的其他位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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