【问题标题】:Rails/Devise---Password Reset & Resend Confirmation How To Redirect to HomepageRails/Devise---密码重置和重新发送确认如何重定向到主页
【发布时间】:2014-12-16 03:04:12
【问题描述】:

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

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

我在 (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


【解决方案1】:
  1. 如何更改重置密码指令的重定向以转到主页,而不是用户登录页面。

只需将其放入您的 passwords 控制器中

protected 
def after_sending_reset_password_instructions_path_for(resource_name)
  root_path
end

  1. 如何更改重新发送确认指令时的重定向以转到主页,而不是用户登录页面。

创建新的confirmations_controller.rb 并将其放入

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

在路线中

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

  1. 如何使用户无法再访问用户/登录页面

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

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

【讨论】:

  • 上述实施后仍有问题。出于某种原因,Devise::PasswordsController 仍会处理重置密码指令,并且重定向仍会转到localhost:3000/users/sign_in。重新发送的确认指令也是如此,它由 Devise::SessionsController 处理并重定向到localhost:3000/users/sign_in。最后,用户仍然可以访问 user/sign_in 而不会重定向到主页。关于可能导致这种情况的任何想法?还将使用我的整个 routes.rb 文件编辑帖子。
  • 不是上面的 Devise::SessionsController 而是 Devise::ConfirmationsController。
猜你喜欢
  • 2012-07-19
  • 2017-10-07
  • 2012-07-19
  • 2011-08-19
  • 1970-01-01
  • 1970-01-01
  • 2012-09-23
  • 1970-01-01
  • 2014-11-23
相关资源
最近更新 更多