【问题标题】:I am using Devise, the password change is redirecting to home page, how to keep it on /users/edit?我正在使用 Devise,密码更改正在重定向到主页,如何将其保留在 /users/edit 上?
【发布时间】:2013-07-23 16:52:38
【问题描述】:

我正在使用设计,视图文件是/devise/registrations/edit.html.erb(我没有对其进行任何更改):

<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>

当用户更改密码时,他们会被重定向到root_url(主页)。我想将它们保留在更改密码页面,即/users/edit。我该怎么做?

EDIT - 我确实有registration_controller 和edit 方法,我应该在里面添加什么?

【问题讨论】:

  • 你的控制器代码会很有用。
  • @vinodadhikary - 我确实有带有编辑方法的registration_controller,我应该在其中添加什么?

标签: ruby-on-rails ruby-on-rails-3 devise


【解决方案1】:

PasswordsController 中的 update 操作调用名为 after_resetting_password_path_for 的受保护方法。

该方法只调用after_sign_in_path_for,所以我认为继承PasswordsController 并覆盖该方法应该是安全的。

看起来已经有一个 test 用于何时覆盖此方法,因此看起来它确实受到支持。

【讨论】:

    【解决方案2】:

    首先,OP在更改密码后出现重定向问题,设计中的更改密码在RegistrationsController,而PasswordsController用于“重置密码”。仅供参考@amesee 重置密码后重定向的答案。修改密码和重设密码是不一样的

    How To: Customize the redirect after a user edits their profile 并查看after_update_path_for(resource)

    您应该在 registrations_controller.rb 上添加 after_update_path_for(resource) 方法,如下所示:

    class RegistrationsController < Devise::RegistrationsController
    
      protected
    
        def after_update_path_for(resource)
          root_path
        end
    end
    

    【讨论】:

    • 感谢完美,只是想知道为什么我们使用受保护的?
    • after_update_path_for(resource) 是非公共方法,子句将阻止用户作为正常操作访问过滤器。
    • 谢谢,还有一件事,我没有看到任何提示密码已更新的消息?我正在使用“edit_user_registration_path”,并且在 edit.html.erb 中有“”?
    • @iCyborg : &lt;%= devise_error_messages! %&gt; 用于错误消息而不是通知,如果您想显示 Flash 消息通知,请将其添加到 edit.html.erb&lt;%= notice %&gt;
    • 我的显示更改密码,然后当我打开新的密码重置电子邮件时,我会被重定向
    【解决方案3】:

    如果你有不同的用户和管理员模型,你需要这个:

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-13
      • 1970-01-01
      • 2019-04-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多