简体   繁体   中英

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM