2015-05-07 22 views
0

我修改了設計代碼,以便我可以通過自定義字段而不是電子郵件來驗證用戶。Rails 4:設計將authentication_key更新爲nil。其餘的自定義字段正在更新。不知道爲什麼?

* 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! } 

出於測試目的,我格外醒目領域所接受。

* 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 

現在,當我嘗試從/users/edit更新用戶信息,它使ACCOUNT_NUMBER nil

以下是應用程序跟蹤:

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) 

正如你所看到的["account_number", nil],。我不確定,爲什麼?

任何幫助表示讚賞。

回答

0

找到了答案。由於字段類型是整數,因此我在devise.rb中標記了下面的字段,使字段nil被提交。評論者解決了這個問題。

# 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 ] 
相關問題