用户如何在Devise中登录或登录后自定义路由?

问题描述:

目前,用户在登录/登录后被重定向到根索引页面,但我想对其进行自定义,以便重定向转到其他页面。用户如何在Devise中登录或登录后自定义路由?

我的路线文件是:

Rails.application.routes.draw do 
    devise_for :admins, path: 'admins' 
    root 'home#index' 
    get '/' => "courses#index", as: :user_root 
    devise_for :users, path: 'users' 
    resources :courses, :lessons 
end 

据我所知,在默认情况下,根成为其中重定向去,所以我使用的代码get '/' => "courses#index", as: :user_root和重定向工作,因为我想。但是,当用户注销时,重定向尝试再次转到get '/' => "courses#index", as: :user_root,我不想这么做。相反,我想在用户注销时重定向到root 'home#index'

所以基本上我的问题是我如何定制我的根,以便我可以实现不同的重定向取决于用户是否登录/登录和注销?

您可以使用此:

class ApplicationController < ActionController::Base 

private 
    def after_sign_in_path_for(resource) 
    user_root_path 
    end 

    def after_sign_out_path_for(resource_or_scope) 
    root_path 
    end 
end 

这是色器件维基:

signing in

signing out