I am using Devise, the password change is redirecting to home page, how to keep it on /users/edit?

霸气de小男生 提交于 2019-12-23 08:25:09

问题


I am using devise and the views file is /devise/registrations/edit.html.erb (I have not made any changes to it):

<div><%= f.label :password %>
<%= f.password_field :password, :autocomplete => "off" %></div>

<div><%= f.label :password_confirmation %>
<%= f.password_field :password_confirmation %></div>

<% if f.object.encrypted_password.present? %>
   <div><%= f.label :current_password %> <i>(we need your current password to confirm your changes)</i><br />
   <%= f.password_field :current_password %></div>
<% end %>
<div><%= f.submit "Update" %></div>

When the user changes their password, they are getting redirected to root_url (homepage). I want to keep them at the change password page, which is /users/edit. How can I do it?

EDIT - I do have registration_controller with edit method, what should I add in it ?


回答1:


First, OP have problem about redirect after change password, change password in devise is in the RegistrationsController, while PasswordsController for "Reset Password". FYI @amesee's answer for redirect after reset password. Change password and reset password are differents

How To: Customize the redirect after a user edits their profile and see after_update_path_for(resource)

You should add after_update_path_for(resource) method on your registrations_controller.rb looks like :

class RegistrationsController < Devise::RegistrationsController

  protected

    def after_update_path_for(resource)
      root_path
    end
end



回答2:


The update action in PasswordsController calls a protected method named after_resetting_password_path_for.

The method just calls after_sign_in_path_for so I think it should be safe to subclass PasswordsController and override this method.

It looks like there's is already a test for whent this method is overridden so it looks like it's definitely supported.




回答3:


If you have different model for User and Admin, you need this:

routes.rb

devise_for :admins, controllers: {registrations: 'admins/registrations'}, defaults: { format: 'html' }

app/controllers/admins/registration_controller.rb

class Admins::RegistrationsController < Devise::RegistrationsController
  def after_update_path_for(resource)
    after_change_password_path # change this
  end
end


来源:https://stackoverflow.com/questions/17816315/i-am-using-devise-the-password-change-is-redirecting-to-home-page-how-to-keep

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!