Ruby on rails 为多个模型设计登录后重定向
Ruby on rails 为多个模型设计登录后重定向,ruby-on-rails,devise,Ruby On Rails,Devise,Rails 5.2 我有两个设计模型用户、工人 用户和工作者有各自独立的设计生成视图 我为(资源) class ApplicationController
Rails 5.2
我有两个设计模型用户、工人
用户和工作者有各自独立的设计生成视图
我为(资源)
class ApplicationController
但是,无论配置如何,用户在登录时都会重定向到根路径
有什么我遗漏的吗?在你的路线中,你可以这样做
devise_for :users, controllers: {
sessions: 'users/sessions',
}
devise_for :workers, controllers: {
sessions: 'workers/sessions',
registrations: 'workers/registrations'
}
然后,您可以在/app/controllers/users/sessions_controller.rb-/app/controllers/workers/sessions_controllers.rb等中创建相应的文件,具体取决于您在routes.rb文件中的描述
然后,在这些文件中,您可以实现如下自定义方法:
class Users::SessionsController < Devise::SessionsController
# before_action :configure_sign_in_params, only: [:create]
# GET /resource/sign_in
# def new
# super
# end
# POST /resource/sign_in
# def create
# super
# end
# DELETE /resource/sign_out
# def destroy
# super
# end
def after_sign_in_path_for(resource)
super(resource)
users_root_path # or whatever path you want here
end
# protected
# If you have extra params to permit, append them to the sanitizer.
# def configure_sign_in_params
# devise_parameter_sanitizer.permit(:sign_in, keys: [:attribute])
# end
end
class用户::sessioncontroller
对于每个资源,这是一个更干净的解决方案,而不是比较类,您可以自由地为特定资源自定义/重写其中的任何方法,而不会使代码复杂化
我希望这能有所帮助。也许是资源。你是一个?而不是普通的电话?我认为后者返回user对象,因此它不等于user。
class Users::SessionsController < Devise::SessionsController
# before_action :configure_sign_in_params, only: [:create]
# GET /resource/sign_in
# def new
# super
# end
# POST /resource/sign_in
# def create
# super
# end
# DELETE /resource/sign_out
# def destroy
# super
# end
def after_sign_in_path_for(resource)
super(resource)
users_root_path # or whatever path you want here
end
# protected
# If you have extra params to permit, append them to the sanitizer.
# def configure_sign_in_params
# devise_parameter_sanitizer.permit(:sign_in, keys: [:attribute])
# end
end