【发布时间】:2014-12-16 03:04:12
【问题描述】:
使用 Devise 2.1.2---最近将登录表单从专用登录页面移至主页。这有一些副作用,我试图在下面弄清楚。
- 如何更改重置密码指令的重定向以转到主页,而不是用户登录页面。
- 如何更改重新发送确认指令时的重定向以转到主页,而不是用户登录页面。
- 如何使用户无法再访问用户/登录页面
我在 (1) 处的尝试如下所示。我猜我应该能够用(2)做类似的事情。不太清楚如何最好地处理(3)——只是删除视图,覆盖另一个设计控制器,等等......
如果您想自己查看重定向的行为,我在 www.ninjaspeak.com 上提供了网站的生产版本。老用户登录页面为http://www.ninjaspeak.com/users/sign_in。
按照此处的说明:Redirect URL after sending reset password instructions 在 git hub 上尝试进行操作,以便在发送重置密码说明时重定向到主页而不是登录页面。
我创建了以下 passwords_controller.rb 文件:
class PasswordsController < Devise::PasswordsController
protected
def after_sending_reset_password_instructions_path_for(resource_name)
root_path
end
end
并在我的 routes.rb 文件中添加了以下行:
devise_for :users, :controllers => { :passwords => "passwords" }
当我运行 rake 路线时,我得到以下信息:
当我点击重置密码说明按钮时,我仍然会被重定向到登录页面而不是根页面。有什么想法吗?
Rails S 输出:
从上面看,似乎仍在使用设计密码控制器,这解释了为什么重定向仍然会转到用户登录页面。
编辑
routes.rb
SampleApp::Application.routes.draw do
devise_for :users do get '/users/sign_out' => 'devise/sessions#destroy' end
devise_for :users, :controllers => { :passwords => "passwords" , :confirmations => "confirmations" }
get 'users/sign_in' => redirect("/")
resources :langs do
collection do
get 'results'
end
end
root to: 'static_pages#home'
match '/about', to: 'static_pages#about'
match '/contact', to: 'static_pages#contact'
match '/news', to: 'static_pages#news'
end
passwords_controller.rb
class PasswordsController < Devise::PasswordsController
protected
def after_sending_reset_password_instructions_path_for(resource_name)
root_path
end
end
confirmations_controller.rb
class ConfirmationsController < Devise::ConfirmationsController
protected
def after_resending_confirmation_instructions_path_for(resource_name)
root_path
end
end
耙路线:
matt@matt-desktop:~/Documents/Ruby/rails_projects/ninja_speak_app$ rake routes
users_sign_out GET /users/sign_out(.:format) devise/sessions#destroy
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
user_password POST /users/password(.:format) devise/passwords#create
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
user_registration POST /users(.:format) devise/registrations#create
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
user_confirmation POST /users/confirmation(.:format) devise/confirmations#create
new_user_confirmation GET /users/confirmation/new(.:format) devise/confirmations#new
GET /users/confirmation(.:format) devise/confirmations#show
new_user_session GET /users/sign_in(.:format) devise/sessions#new
POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
POST /users/password(.:format) passwords#create
GET /users/password/new(.:format) passwords#new
GET /users/password/edit(.:format) passwords#edit
PUT /users/password(.:format) passwords#update
GET /users/cancel(.:format) devise/registrations#cancel
POST /users(.:format) devise/registrations#create
GET /users/sign_up(.:format) devise/registrations#new
GET /users/edit(.:format) devise/registrations#edit
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
POST /users/confirmation(.:format) confirmations#create
GET /users/confirmation/new(.:format) confirmations#new
GET /users/confirmation(.:format) confirmations#show
users_sign_in GET /users/sign_in(.:format) :controller#:action
results_langs GET /langs/results(.:format) langs#results
langs GET /langs(.:format) langs#index
POST /langs(.:format) langs#create
new_lang GET /langs/new(.:format) langs#new
edit_lang GET /langs/:id/edit(.:format) langs#edit
lang GET /langs/:id(.:format) langs#show
PUT /langs/:id(.:format) langs#update
DELETE /langs/:id(.:format) langs#destroy
root / static_pages#home
about /about(.:format) static_pages#about
contact /contact(.:format) static_pages#contact
news /news(.:format) static_pages#news
【问题讨论】:
-
如果您在 after_sending_reset_password_instructions_path_for 设置断点并重置您的密码,它是否会停止您放置断点的位置?您可以发布控制台日志以检查重定向吗?几个月前我做了和你一模一样的事情,而且效果很好
-
添加了控制台输出。我明天去查断点,我以前从来没用过。
标签: ruby-on-rails ruby devise