发送重置密码指令后重定向

时间:2014-09-29 09:40:49

标签: ruby-on-rails devise

我知道,我可以在此wiki How To: Redirect URL after sending reset password instructions

之后的应用上创建PasswordsController

我的问题是:

为什么我不能将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

我确定,我的路线中有user_login_path .rb

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)

1 个答案:

答案 0 :(得分:1)

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