简体   繁体   中英

Rails 4: Devise updating authentication_key to nil. Rest custom fields are updating fine. Not sure why?

I modified devise code so that i can authenticate user via a custom field instead of email.

* ApplicationController.rb *

 devise_parameter_sanitizer.for(:sign_in) { |u| u.permit! }
 devise_parameter_sanitizer.for(:sign_up) { |u| u.permit! }
 devise_parameter_sanitizer.for(:account_update) { |u| u.permit! }

For testing purposes i marked all fields as accepted.

* devise.rb *

config.authentication_keys = [ :account_number ]

* user.rb *

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable, :omniauthable, :registerable, :recoverable
  devise :database_authenticatable, :rememberable, :trackable, :validatable, :registerable
  def email_required?
    false
  end

  def email_changed?
    false
  end

Now when I try to update user information from /users/edit it makes the account_number nil

Here is the application trace:

Started PUT "/users" for ::1 at 2015-05-06 21:35:19 -0500
Processing by Devise::RegistrationsController#update as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"jpEsK0+LhiNLFDH5ELyd4H/ukmQL853kAQdHeSAN6F2cmNPkt/BHFDKTpkuYkLbhr4U4xnqmruCueqoJ+Lazfg==", "user"=>{"custom_number"=>"1234567", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "current_password"=>"[FILTERED]", "account_number"=>"710803996"}, "commit"=>"Update"}
  User Load (0.3ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ?  ORDER BY "users"."id" ASC LIMIT 1  [["id", 2]]
  User Load (0.1ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", 2]]
   (0.1ms)  begin transaction
  SQL (0.4ms)  UPDATE "users" SET "custom_number" = ?, "encrypted_password" = ?, "account_number" = ?, "updated_at" = ? WHERE "users"."id" = ?  [["custom_number", 1234567], ["encrypted_password", "$2a$10$.e7Gz8FyhRAUhcmUSLjEKOi70N3YlmixrbRljX6jCWoyT/e.TeuJi"], ["account_number", nil], ["updated_at", "2015-05-07 02:35:19.424611"], ["id", 2]]
   (1.3ms)  commit transaction
Redirected to http://localhost:3000/
Completed 302 Found in 145ms (ActiveRecord: 2.2ms)

As you can see ["account_number", nil], . And I am not sure, why?

Any help is appreciated.

Found the answer. Since the field type was integer, I had the below marked in devise.rb , making the field nil on submission. Commenting those solved the issue.

  # Configure which authentication keys should be case-insensitive.
  # These keys will be downcased upon creating or modifying a user and when used
  # to authenticate or find a user. Default is :email.
  config.case_insensitive_keys = [ :account_number ]

  # Configure which authentication keys should have whitespace stripped.
  # These keys will have whitespace before and after removed upon creating or
  # modifying a user and when used to authenticate or find a user. Default is :email.
  config.strip_whitespace_keys = [ :account_number ]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM