Ruby on rails 用户登录或登录Desive后,如何自定义路由?
目前,用户在登录/登录后会被重定向到根索引页,但我想对其进行自定义,以便重定向到其他页面 我的路由文件是:Ruby on rails 用户登录或登录Desive后,如何自定义路由?,ruby-on-rails,ruby,devise,Ruby On Rails,Ruby,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,
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”,作为::user_root
,重定向按照我的意愿工作。但是,当用户注销时,重定向会再次尝试转到get'/'=>“courses#index”,如::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
class ApplicationController
它位于Desive wiki上:
您可以使用以下功能:
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
class ApplicationController
它位于Desive wiki上: