ruby-on-rails - 发送重置密码指令后重定向设计

标签 ruby-on-rails devise

我知道,我可以按照这个 wiki 在我的应用程序上创建 PasswordsController How To: Redirect URL after sending reset password instructions

我的问题是:

为什么我不能动after_sending_reset_password_instructions_path_for(resource_name) ApplicationController 的方法看起来像:

protected
# works
def after_sign_in_path_for(resource)
  root_path    
end

# works
def after_sign_out_path_for(resource_or_scope)
  root_path 
end

# doesn't work    
def after_sending_reset_password_instructions_path_for(resource_name)
  user_login_path
end

仍然重定向到默认路径 localhost:3000/users/sign_in

我确定,我的 routes.rb 中有 user_login_path

devise_scope :user do
  get '/register' => 'devise/registrations#new', :as => :user_register
  get '/user/login' => 'devise/sessions#new', :as => :user_login
  delete '/user/logout' => 'devise/sessions#destroy', :as => :user_logout
  get '/forgot' => 'devise/passwords#new', :as => :user_forgot
  post '/send/instruction' => 'devise/passwords#create', :as => :forgot_instructions
  get '/new/password' => 'devise/passwords#edit', :as => :forgot_new
  put '/update/password' => 'devise/passwords#update', :as => :forgot_update
end

当我发送指令重置密码时,这个堆栈跟踪:

Started POST "/send/instruction" for 127.0.0.1 at 2014-09-29 16:32:12 +0700
Processing by Devise::PasswordsController#create as HTML
  Parameters: {"utf8"=>"V", "authenticity_token"=>"S7AppqJSUKat7GwhH8U/VhzQXvibiSaM1z6QJi+aP8s=", "u
ser"=>{"email"=>"myemail@gmail.com"}, "commit"=>"Send me reset password instructions"}
  ←[1m←[35mUser Load (3.0ms)←[0m  SELECT  "users".* FROM "users"  WHERE "users"."email" = 'myemail@
gmail.com'  ORDER BY "users"."id" ASC LIMIT 1
  ←[1m←[36mUser Load (1.0ms)←[0m  ←[1mSELECT  "users".* FROM "users"  WHERE "users"."reset_password_
token" = 'ee0dd49a2d7e4ba688e08ddebe40399c8f5a59ae5d8083621c35be09adf4aa65'  ORDER BY "users"."id" A
SC LIMIT 1←[0m
  ←[1m←[35m (1.0ms)←[0m  BEGIN
  ←[1m←[36mSQL (0.0ms)←[0m  ←[1mUPDATE "users" SET "reset_password_sent_at" = $1, "reset_password_to
ken" = $2, "updated_at" = $3 WHERE "users"."id" = 2←[0m  [["reset_password_sent_at", "2014-09-29 09:
32:13.779043"], ["reset_password_token", "ee0dd49a2d7e4ba688e08ddebe40399c8f5a59ae5d8083621c35be09ad
f4aa65"], ["updated_at", "2014-09-29 09:32:13.780043"]]
  ←[1m←[35m (11.0ms)←[0m  COMMIT
  Rendered devise/mailer/reset_password_instructions.html.erb (1.0ms)

Devise::Mailer#reset_password_instructions: processed outbound mail in 119.0ms

Sent mail to  myemail@gmail.com (5611.3ms)
Date: Mon, 29 Sep 2014 16:32:13 +0700
From: please-change-me-at-config-initializers-devise@example.com
Reply-To: please-change-me-at-config-initializers-devise@example.com
To: myemaik@gmail.com
Message-ID: <5429271df3e90_1534f9a9b0166f0@ASUS-PC.mail>
Subject: Reset password instructions
Mime-Version: 1.0
Content-Type: text/html;
 charset=UTF-8
Content-Transfer-Encoding: 7bit

<p>Hello myemail@gmail.com!</p>

<p>Someone has requested a link to change your password. You can do this through the link below.</p>


<p><a href="http://localhost:3000/new/password?reset_password_token=y4od6w4JsDnWeUbHTkyp">Cha
nge my password</a></p>

<p>If you didn't request this, please ignore this email.</p>
<p>Your password won't change until you access the link above and create a new one.</p>

Redirected to http://localhost:3000/users/sign_in
Completed 302 Found in 6791ms (ActiveRecord: 17.0ms)

最佳答案

因为 sign_in 和 sign_out 方法是公开的,并且方法 after_sending_reset_password_instructions_path_for受到保护。您需要创建一个继承自 Devise::PasswordController 的 Controller 。希望这会有所帮助。

关于ruby-on-rails - 发送重置密码指令后重定向设计,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26096855/

相关文章:

ruby-on-rails - current_user 的 default_scope

ruby-on-rails - 如何有效地检查一组记录的 updated_at 和 created_at 是否相等

ruby-on-rails - JoyentOS libcurl 与乘客

mysql - 带有 Rails + Apache/Passenger 和 MySql 的 Linux Mint

ruby-on-rails - RSpec 共享示例不得共享

ruby-on-rails - 设计使用什么密码哈希算法?

ruby-on-rails - 当现有用户尝试注册时登录 - 设备

facebook - OmniAuth + Devise - Facebook 提供商 - OAuthException 191

ruby-on-rails - 注册时出现Devise Gem错误

ruby-on-rails - 为多个用户设计 session