gpt4 book ai didi

ruby-on-rails - Rails/Devise---密码重置和重新发送确认如何重定向到主页

转载 作者:太空宇宙 更新时间:2023-11-03 18:15:10 24 4
gpt4 key购买 nike

使用 Devise 2.1.2——最近将登录表单从专用登录页面移至主页。这有一些副作用,我试图在下面弄清楚。

  1. 如何将重置密码指令的重定向更改为转到主页,而不是用户登录页面。
  2. 如何将重新发送确认指令的重定向更改为转到主页,而不是用户登录页面。
  3. 如何让用户无法再访问用户/登录页面

我对 (1) 的尝试如下所示。我猜我应该能够做与 (2) 类似的事情。不太确定如何最好地处理 (3)---只需删除 View 、覆盖另一个设计 Controller 等...

如果您想自己查看重定向的行为,我在 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 routes 时,我得到以下信息:

enter image description here

当我点击重置密码说明按钮时,我仍然被重定向到登录页面而不是根页面。想知道这是为什么吗?

Rails S 输出:

enter image description here

从上面看,设计密码 Controller 似乎仍在使用,这解释了为什么重定向仍会转到用户登录页面。

编辑

路线.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

密码 Controller .rb

class PasswordsController < Devise::PasswordsController
protected
def after_sending_reset_password_instructions_path_for(resource_name)
root_path
end
end

确认 Controller .rb

class ConfirmationsController < Devise::ConfirmationsController
protected
def after_resending_confirmation_instructions_path_for(resource_name)
root_path
end
end

rake 路:

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

最佳答案

  1. How to change redirect on reset password instructions to go to home page, not user sign in page.

只需将其放入您的密码 Controller

protected 
def after_sending_reset_password_instructions_path_for(resource_name)
root_path
end

  1. How to change redirect on resend confirmation instructions to go to home page, not user sign in page.

创建新的 confirmations_controller.rb 并放置它

class ConfirmationsController < Devise::ConfirmationsController
protected
def after_resending_confirmation_instructions_path_for(resource_name)
root_path
end
end

在 route

 devise_for :users, :controllers => { :passwords => "passwords" , :confirmations => "confirmations" }

  1. How to make it so users can no longer access users/sign_in page

简单的技巧是将用户重定向到主页,因为您的网站正在制作中。删除路径可能会导致一些问题,所以最好这样做

 get 'users/sign_in' => redirect("/")

边注:

如果你只想在根页面登录,你可以使用这个

  root :to => "devise/sessions#new"

更新

你必须从 route.rb 文件中删除这一行

 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("/")

并添加这个

  get 'users/sign_in' => redirect("/")
get '/users/sign_out' => 'devise/sessions#destroy'
devise_for :users, :controllers => { :passwords => "passwords" , :confirmations => "confirmations" }

关于ruby-on-rails - Rails/Devise---密码重置和重新发送确认如何重定向到主页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27496856/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com