如何在Devise中更改全局变量

时间:2017-12-20 18:57:31

标签: ruby-on-rails devise

我想改变它(在devise.rb中定义):

@@reset_password_keys = [:email]

为:

@@reset_password_keys = [:email, :role]

最终使用(required_attributes)在authenticatable.rb中定义的以下方法中查找用户。

我想使用它来强制从重置密码表单发送emailrole属性。

用法:

def find_or_initialize_with_errors(required_attributes, attributes, error=:invalid) #:nodoc:
  attributes = if attributes.respond_to? :permit!
    attributes.slice(*required_attributes).permit!.to_h.with_indifferent_access
  else
    attributes.with_indifferent_access.slice(*required_attributes)
  end
  attributes.delete_if { |key, value| value.blank? }

  if attributes.size == required_attributes.size
    record = find_first_by_auth_conditions(attributes)
  end

  unless record
    record = new

    required_attributes.each do |key|
      value = attributes[key]
      record.send("#{key}=", value)
      record.errors.add(key, value.present? ? error : :blank)
    end
  end

  record
end

1 个答案:

答案 0 :(得分:1)

您应该在config/initializers/devise.rb文件中更改它:

config.reset_password_keys = [:email, :role]

https://github.com/plataformatec/devise/blob/88724e10adaf9ffd1d8dbfbaadda2b9d40de756a/test/rails_app/config/initializers/devise.rb#L127