2016-09-29 5 views
0

Railsアプリケーションでは、config Deviseファイルにreset_password_keysオプションがあります。キーの1つをオプションにする方法はありますか?Devitsにオプションのreset_password_keysを設定する方法

現在、この設定はconfig.reset_password_keys = [ :email, :account_id ]です。

account_idが存在しない場合は、account_idをオプションのキーにします。

答えて

0

Deviseのwikiにentryがあります。

# on config/initializers/devise.rb: 
config.reset_password_keys = [ :email, :account_id ] 

# on User model 
def self.find_first_by_auth_conditions(warden_conditions) 
    conditions = warden_conditions.dup 
    where(account_id: conditions[:account_id], email: conditions[:email]).first 
end 

# on passwords/new.html.erb 
<%= f.label :email %><br /> 
<%= f.text_field :email %></p> 
<%= f.label :account_id %><br /> 
<%= f.text_field :account_id %></p> 
+0

は私が忘れてしまった私のパスワード、User.rbとAdmin.rbを上のクリックできる2つのモデルを持っている は、私はあなたが次の操作を行うことをお勧めしたいです。ユーザーがパスワードをリセットしているときは、必要な電子メールとアカウントIDを持っている必要がありますが、アカウントIDを持たないため、管理者の電子メールのみを使用することができます。 –

+0

モデルに基づいてconfig.reset_password_keysを異なるキーに設定する方法はありますか?ユーザーの場合、config.reset_password_keys = [:email、:account_id]、および管理者config.reset_password_keys = [:email]の場合 –

+1

ああ..今私はそれを得る。 User.rbでは、emailとaccount_idの両方が必要です。私はこれを試していませんが、 'User.rb'と' Admin.rb'で '.find_first_by_auth_conditions'をオーバーライドする必要があると思います。私はあなたの配列[:email、:account_id]に固執して、Adminモデルの 'account_id'を無視することができると思います。 –

1

私のために働い以下:

#Admin.rb devise :database_authenticatable, :encryptable, :recoverable, :rememberable, :trackable, :reset_password_keys => [:email]

関連する問題