Awesomeness
Awesomeness

Reputation: 2631

Rails devise - error on update when resetting the password

I have this controller code:

  def update

    super
    respond_to do |format|
      format.html{redirect_to session[:redirect_to]}
    end
  end

My class inherits from devise Passwords like this:

class Mobile::PasswordsController < Devise::PasswordsController

and I get this error:

AbstractController::DoubleRenderError in Mobile::PasswordsController#update

    Render and/or redirect were called multiple times in this action. 
Please note that you may only call render OR redirect, and at most once per action. 
Also note that neither redirect nor render terminate execution of the action, so 
if you want to exit an action after redirecting, you need to do something like 
"redirect_to(...) and return".

Any idea what to do? I know its the redirects, but I am not sure how to make them right.

Thanks!

Upvotes: 0

Views: 1467

Answers (1)

Rustam Gasanov
Rustam Gasanov

Reputation: 15791

The update method of the Devise gem already has a redirect. Then you call your redirect and this causes the error. If you want to do a custom redirect you need to override a Devise method without a super call like this:

def update
    self.resource = resource_class.reset_password_by_token(params[resource_name])

    if resource.errors.empty?
      flash_message = resource.active_for_authentication? ? :updated : :updated_not_active
      set_flash_message(:notice, flash_message) if is_navigational_format?
      sign_in(resource_name, resource)
      respond_with resource, :location => #your_path_on_success
    else
      flash[:error] = resource.errors.full_messages
      redirect_to #your_path_on_failure
    end
  end

Upvotes: 2

Related Questions