Rails 4 Devise Multiple User Models STI

霸气de小男生 提交于 2019-12-04 12:46:36

You can also use the easy_roles gem. It is available on github. This gem provides a bitmask solution for your different user roles. You just must have one model, e.g User, this model gets an attribute "role". Just checkout the docs on github. Devise + easy_roles + CanCan is a very good setup, it is very convenient in my opinion. I use this quite often.

Link to github: https://github.com/platform45/easy_roles

I'm not sure this really answers the original question. I am trying to set up multiple session controllers for Devise, and it seems like it really does require multiple models for this use case. Will post back when I've got a working version.

Yimanei

STI will give you current_admin, current_user1 and current_user2 Devise methods.

In application_controller.rb, create a custom current_user method like this:

  def current_user
    if current_admin
      current_admin
    elsif current_user1
      current_user1
    else
      current_user2
    end
  end
helper_method :current_user

You will need some work on routes.rb and a custom sessions_controller.rb. See the accepted answer here Rails: Using Devise with single table inheritance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!