如何为两个设计模型使用不同的authentication_keys?

Roy*_*Roy 25 authentication models devise ruby-on-rails-3

现在我有一个设计模型,它使用email作为authentication_key.

我想添加一个新的设计模型,它使用student_id作为authentication_key.

一些指南告诉我修改配置

"config.authentication_keys = [:email]"通过将:email替换为:student_id.

修改后,第一个模型登录总是失败,所以我想我必须分别为两个模型指出不同的authentication_keys.

我应该怎么做?

Dim*_*ris 79

您必须在模型内部声明authenitcation键,而不是在devise.rb文件中.

class model1 < ActiveRecord::Base

devise :database_authenticatable, :rememberable, :trackable, :authentication_keys => [:email]
Run Code Online (Sandbox Code Playgroud)

并为您的第二个模型

class model2 < ActiveRecord::Base

devise :database_authenticatable, :rememberable, :trackable, :authentication_keys => [:studentid]
Run Code Online (Sandbox Code Playgroud)

还要确保从devise.rb注释掉config.authentication_keys设置

  • 重要提示:如果您使用的是Recoverable,请确保添加````:reset_password_keys => [:email]````,否则密码重置将始终作用于:email(!) (3认同)
  • 罗伊,如果答案合适,那么请随意将其标记为正确的答案(我正在努力建立一些声誉!).非常感谢! (2认同)