Search code examples
ruby-on-railsactiverecorddevisemultiple-users

different Models with devise


Is it possible to generate different models with devise and have different signup forms each?

I need to have 2types of user who can both login, signup etc, but they accept different parameters.

I tried to create different models, but into my views still give me only one folder with a unique signup form. I also tried to put into this form all the parameters I have for both type users, but it returns

  undefined method `philosophy' for #<User:0x007fbab0961ca8>

where philosophy is the parameter of my second user.

I would need this to work because I'm trying to associate this 2 users one to another through active record

   user belongs_to :charity

   charity has_many :users

Any idea how I can solve this?


Solution

  • To generate multiple models (say, User and Admin), just use:

    > rails generate devise User
    > rails generate devise Admin
    

    To generate different sets of views:

    Set config.scoped_views = true inside the config/initializers/devise.rb file. You can now have views based on the role like users/sessions/new and admins/sessions/new. If no view is found within the scope, Devise will use the default view at devise/sessions/new. You can use the generator to generate scoped views:

    $ rails generate devise:views users
    $ rails generate devise:views admins
    

    Source: devise github readme