I’m trying to pull the change password fields into a separate form, but get an error with method devise_error_message!.
If I remove the method updating workings fine, however, if a validation fails it redirects to the registrations/edit.html.erb and gives the error messages. How would I get it to redirect back to the registrations/change_password.html.erb view and give the devise_error_messages! ?
All my code :
http://pastie.org/1907545
There’s a few things that I think need to be resolved.
1) Initialize the resource in the change_password action
The call to
devise_error_messages!fails because you aren’t initializing theresource(your User model instance) in theRegistrationsController#change_passwordaction. One way to do this is to make sure that theauthenticate_scope!before filter, which is implemented in Devise::RegistrationsController, gets called on thechange_passwordaction. Try something like this in your RegistrationsController.If that doesn’t work, you might want to simply call
authenticate_scope!at the beginning of yourchange_passwordaction.2) Redirect to change_password.html.erb in case of a failure
Basically both of the
Devise::RegistrationsController#editaction and yourRegistrationsController#change_passwordaction submit a form to theDevise::RegistrationsController#updateaction. What you want to do is make sure that when an update fails, if the form submission is coming from theDevise::RegistrationsController#editaction then you render theregistrations/edit.html.erbview and similarly if the form submission is coming from theRegistrationsController#change_passwordaction then you renderregistrations/change_password.html.erbview.There are various ways to do this including relying on the flash hash to set a key in the
RegistrationsController#change_passwordaction (e.g.flash[:change_password] = true) and then check for the presence of this key if an error occurs during an update. Another approach would be to use a hidden field in your change_password form then similarly, if an error occurs during an update, check for the presence of this hidden field in the params hash. Something like this.Either way you will need to override the Devise::RegistrationsController#udpate action. Something like this:
Give this a try but I think that should put you back on track.