2015-04-30 41 views

回答

2

Reset password token is invalid消息是在更新密碼時引發的驗證錯誤,而不是設計特定錯誤(其中消息存儲在devise.en.yml中)。

此驗證發生在devise/passwords_controller#update方法中。 代碼包括下面:

# PUT /resource/password 
def update 
    self.resource = resource_class.reset_password_by_token(resource_params) 
    yield resource if block_given? 

    if resource.errors.empty? 
    resource.unlock_access! if unlockable?(resource) 
    flash_message = resource.active_for_authentication? ? :updated : :updated_not_active 
    set_flash_message(:notice, flash_message) if is_flashing_format? 
    sign_in(resource_name, resource) 
    respond_with resource, location: after_resetting_password_path_for(resource) 
    else 
    respond_with resource 
    end 
end 

self.resource = resource_class.reset_password_by_token(resource_params)線設置與向reset_password_token是無效相關的錯誤消息resource.errors對象。

此行之後檢查的resource.errors值將顯示一個大哈希與... @messages={:reset_password_token=>["is invalid"]}

devise_error_messages method重新格式化這個結尾說的「重設密碼令牌是無效的」。

要更改此消息,應該自定義密碼控制器,並且update方法更改爲具有不同的錯誤消息哈希。

步驟將如下所示:

1)自定義路線密碼控制器

# config/routes.rb 
devise_for :users, :controllers => { :passwords => "passwords" } 

2)創建的自定義密碼控制器

# app/controllers/passwords_controller.rb 
class PasswordsController < Devise::PasswordsController 

end 

3)自定義更新方法更改錯誤信息:

# app/controllers/passwords_controller.rb 
# Include the update method as it is fully, with the following changes in the else block: 

def update 
    ... 

    if resource.errors.empty? 
    ... 
    else 
    if resource.errors.messages[:reset_password_token] 
     resource.errors.messages.delete(:reset_password_token) 
     resource.errors.messages[:password_reset_link] = ["has already been used"] 
    end 
    respond_with resource 
    end 

進一步瞭解Customizing Devise controllers

1

甲不是覆蓋passwords_controller簡單的解決方案,是簡單地修改視圖:

在應用程序/視圖/設計/口令/ edit.html.haml(或ERB當量), 只是把這個條件在窗體中:

- if resource.errors[:reset_password_token].present? 
    .alert.alert-danger 
    This password reset URL has expired. You may have requested to reset your password more than once. Follow the link in the most recent email or 
    = link_to 'request to reset your password again.', new_user_password_path 

而且你可能想要刪除這兩行:

= f.error_notification 
= f.full_error :reset_password_token