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

时间:2014-12-16 03:04:13

标签: ruby-on-rails ruby devise

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

  1. 如何更改重置密码说明的重定向以转到主页,而不是用户登录页面。
  2. 如何更改重定向重定向以转到主页,而不是用户登录页面。
  3. 如何制作,以便用户无法再访问用户/ sign_in页面
  4. 我对(1)的尝试如下所示。我猜我应该可以用(2)做类似的事情。不太清楚如何最好地处理(3)---只是删除视图,覆盖另一个设计控制器等...

    如果您想自己查看重定向的行为,我可以在www.ninjaspeak.com上找到该网站的生产版本。旧用户登录页面位于http://www.ninjaspeak.com/users/sign_in

    按照此处的说明操作:git hub上的Redirect URL after sending reset password instructions尝试制作,以便在重置密码说明发送时重定向到主页而不是登录页面。

    我创建了以下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路线时,我得到以下内容:

    enter image description here

    当我点击重置密码说明按钮时,我仍然会被重定向到登录页面而不是根页面。对此为何的任何想法?

    Rails S输出:

    enter image description here

    从上面看,似乎仍然使用了设计密码控制器,这解释了为什么重定向仍然会进入用户登录页面。

    修改

    的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
    

    rake routes:

    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 个答案:

答案 0 :(得分:3)

  
      
  1. 如何更改重置密码说明的重定向以转到主页,而不是用户登录页面。
  2.   

将其放入您的passwords控制器

protected 
def after_sending_reset_password_instructions_path_for(resource_name)
  root_path
end

  
      
  1. 如何更改重定向重定向以转到主页,而不是用户登录页面。
  2.   

创建新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. 如何制作,以便用户无法再访问用户/ sign_in页面
  2.   

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

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