Léo Rocha
Léo Rocha

Reputation: 324

Devise - when User Update failure redirect to custom page

How change update user failure route passing devise errors?

Here is my controller code to replace devise registration update:

class RegistrationsController < Devise::RegistrationsController

  def edit
    render :edit
  end

  def update
    self.resource = resource_class.to_adapter.get!(send(:"current_#{resource_name}").to_key)
    prev_unconfirmed_email = resource.unconfirmed_email if resource.respond_to?(:unconfirmed_email)

    resource_updated = update_resource(resource, account_update_params)
    yield resource if block_given?
    if resource_updated
      if is_flashing_format?
        flash_key = update_needs_confirmation?(resource, prev_unconfirmed_email) ?
          :update_needs_confirmation : :updated
        set_flash_message :notice, flash_key
      end
      bypass_sign_in resource, scope: resource_name
      respond_with resource, location: after_update_path_for(resource)
    else
      clean_up_passwords resource
      set_minimum_password_length
      respond_with resource, location: profile_member_users_path(resource)
    end
  end

  def after_update_path_for(resource)
    profile_member_users_path(resource)
  end

end  

I trying change:

else
  clean_up_passwords resource
  set_minimum_password_length
  respond_with resource

To

else
      clean_up_passwords resource
      set_minimum_password_length
      redirect_to profile_member_users_path(resource)

But this don't pass devise errros to the view.

Upvotes: 3

Views: 1231

Answers (2)

Julius Dzidzevičius
Julius Dzidzevičius

Reputation: 11000

You need to cary devise errors with redirect? So then it depends how you use those errors. Devises passes them as a Hash in resource.errors. I just tested a bit and this could be your starting point:

   else
      clean_up_passwords resource
      set_minimum_password_length
      respond_with redirect_to: profile_member_users_path(resource),
                   notice: resource.errors['current_password'][0]

    end

Upvotes: 0

Tom Aranda
Tom Aranda

Reputation: 6036

Since you are redirecting, the current resource data is not being passed to the view. You need to render the view without a redirect. Try replacing:

redirect_to profile_member_users_path(resource)

with

respond_with resource, location: profile_member_users_path(resource)

Of course, this is no different than the original Devise code. So you need to replace profile_member_users_path(resource) with your custom path.

Upvotes: 1

Related Questions