Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using devise for multiple models

The question may not be that complicated, but i am confuse. I have two users i.e. Student and Teacher, and for those i want separate models, controllers and views. I want teacher to use /t/sign_up and for student /s/sign_up. I am using devise for authentication, I know it is possible because that's how active admin works.

like image 739
user464 Avatar asked May 10 '16 18:05

user464


People also ask

What is devise warden?

The devise gem is basically based on a warden gem, which gives an opportunity to build authorization direct on a Ruby Rack Stack. This gem is pretty straightforward and well documented. Warden fetches a request data and checks if the request includes valid credentials, according to a defined strategy.

What is resource in devise?

Resource is an abstraction name of instance of a user. It can be configured in devise settings to work with Admin model or any other. By default it's the first devise role declared in your routes devise :users # resource is instance of User class devise :admins # resource is instance of Admin class.


1 Answers

Provided you have already generated multiple models and views with devise, and just want to change the path name, you can do that configuring config/routes.rb:

devise_for :students, path: 's'
devise_for :teachers, path: 't'

which will replace your routes like this:

http://localhost:3000/s/sign_up
http://localhost:3000/t/sign_up

If you want to have your views based on different models, you can configure config.scoped_views = true inside config/initializers/devise.rb file and generate views for that model:

rails g devise:views students

And if you want to customize each controller, you can generate their controller files like this:

rails generate devise:controllers students

This will create controllers based on model name, thus you can define them in your routes:

devise_for :students, path: 's', controllers: { sessions: "students/sessions" }

like image 114
pyfl88 Avatar answered Oct 05 '22 15:10

pyfl88