简体   繁体   English

rails设计登录模型的检查类

[英]rails devise check class of logged in model

I have multiple models which are using devise like Admin, Student and Counselor 我有多个模型,使用管理员,学生和顾问等设计

I don't want to use roles as it 'll complicate things in this case. 我不想使用角色,因为在这种情况下它会使事情变得复杂。

I want to use one layout and show menus etc. depending upon class of model like 我想使用一个布局和显示菜单等取决于类型的模型

= render "shared/#{resource.class}_menu"

Is there way that I can get class of logged in object without if else conditions like we do in after_sign_in_path_for etc. 有没有像我们在after_sign_in_path_for等那样的条件下我可以获得登录对象的类。

So what I do in this case where you want to have different areas for Admin, Student and Counselor 那么在这种情况下我想要为管理员,学生和顾问提供不同的区域

you can namespace each role or log in like this 您可以为每个角色命名,也可以像这样登录

#config/routes.rb
Rails.application.routes.draw do

  devise_for :admins, :controllers => { registrations:  'admins/registrations',
                                       sessions:        'admins/sessions',
                                       passwords:       'admins/passwords',
                                       confirmations:   'admins/confirmations'
  }
  authenticate :admin do
    namespace :admins do
      ....
      root :to => 'something#index'
    end
  end

  devise_for : students, :controllers => { registrations:   'students/registrations',
                                           sessions:        'students/sessions',
                                           passwords:       'students/passwords',
                                           confirmations:   'students/confirmations'
  }
  authenticate :student do
    namespace :students do
      ....
      root :to => 'something#index'
    end
  end

Now you can create the devise model for each namespace. 现在,您可以为每个命名空间创建设计模型。

#app/controllers/admin_controller.rb
class AdminController < ApplicationController
  before_filter :authenticate_admin!

end


For Devise controllers you can use them like tihs 对于Devise控制器,您可以像使用它们一样使用它们

#app/controllers/users/sessions_controller.rb
class Users::SessionsController < Devise::SessionsController

end

For basic application controllers, you can use them like this. 对于基本的应用程序控制器,您可以像这样使用它们。

#app/controllers/users/somethings_controller.rb
class Users::SomethingsController < UserController
...
end

I hope that this is able to help you 我希望这能够帮到你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM