2015-04-30 14 views

risposta

2

Reset password token is invalid messaggio è un errore di convalida generato durante l'aggiornamento della password e non è un errore specifico elaborato (per il quale i messaggi sono memorizzati in devise.en.yml).

Questa convalida avviene nel metodo devise/passwords_controller#update. codice riportato qui di seguito:

# 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 

La linea self.resource = resource_class.reset_password_by_token(resource_params) imposta l'oggetto resource.errors con il messaggio di errore relativo al reset_password_token essere valido.

Controllo il valore di resource.errors dopo questa linea mostrerà una grande hash termina con ... @messages={:reset_password_token=>["is invalid"]}

I devise_error_messages method riformatta questo per dire "Reset password token non è valido".

Per modificare questo messaggio, il controller delle password deve essere personalizzato e il metodo update modificato per avere un diverso hash del messaggio di errore.

Procedura sarebbero le seguenti:

1) Personalizzare i percorsi per le password controllore

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

2) Creare la password personalizzato controllore

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

end 

3) Personalizzare il metodo di aggiornamento di cambia il messaggio di errore:

# 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 
.210

più su Customizing Devise controllers

1

Una soluzione più semplice di sovrascrivere l'passwords_controller, è semplicemente quello di modificare la vista:

in app/views/ideare/password/edit.html.haml (o il vostro erb equivalente), Basta mettere questa condizione all'interno del modulo:

- 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 

E si consiglia di rimuovere queste due linee:

= f.error_notification 
= f.full_error :reset_password_token 
Problemi correlati