[RUBY] Use devise on multiple models

Implement the authentication process for each user and administrator using devise. The namespace is implemented without dividing it so that the explanations unrelated to devise do not increase, but it seems that it is often better to separate it when actually developing from 0.

See the following article for the basic usage of devise [Rails] Key points for using devise at a minimum --Qiita

install devise


gem 'devise'
$ bundle install
$ rails g devise:install


config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }


<p class="notice"><%= notice %></p>
<p class="alert"><%= alert %></p>

Create Model

$ rails g devise User
$ rails g devise AdminUser
$ rails db:migrate

Create Controller

$ rails g devise:controllers users
$ rails g devise:controllers admin_users

Create View


config.scoped_views = true
$ rails g devise:views users
$ rails g devise:views admin_users

Create routing


Rails.application.routes.draw do
  devise_for :users, controllers: {
    sessions: 'users/sessions',
    passwords: 'users/passwords',
    registrations: 'users/registrations'
  devise_for :admin_users, controllers: {
    sessions: 'admin_users/sessions',
    passwords: 'admin_users/passwords',
    registrations: 'admin_users/registrations'

at this point http://localhost:3000/users/sign_in http://localhost:3000/admin_users/sign_in Access and confirm that the login screen is displayed.

Create a test page

$ rails g controller static_pages index
$ rails g scaffold UserPost title body
$ rails g scaffold AdminUserPost title body
$ rails db:migrate


Rails.application.routes.draw do
  root to: 'static_pages#index'
  resources :admin_user_posts
  resources :user_posts


<%= link_to 'Login', new_user_session_path %>
<%= link_to 'sign up', new_user_registration_path %>
<%= link_to 'Login', new_admin_user_session_path %>
<%= link_to 'sign up', new_admin_user_registration_path %>


<%= link_to 'Log out', destroy_user_session_path, method: :delete %>


<%= link_to 'Log out', destroy_admin_user_session_path, method: :delete %>

Correct the transition destination after login

After logging in as User, go to user_posts # index Set to transition to admin_user_posts # index after logging in as AdminUser.


class ApplicationController < ActionController::Base
  def after_sign_in_path_for(resource)
    case resource
    when User
    when AdminUser

Make it inaccessible without logging in

The User Post screen can be displayed only when logged in as User You can display the AdminUserPost screen only when you are logged in as AdminUser Set as.


class UserPostsController < ApplicationController
  before_action :authenticate_user!


class AdminUserPostsController < ApplicationController
  before_action :authenticate_admin_user!

This time, there is only one controller for both the user screen and the management screen, so there is no problem with this method, but in reality, multiple controllers will be created, so name_space is separated and each application_controller is set to before_action. I think it's better.

Recommended Posts

Use devise on multiple models
[Rails] Manage multiple models using devise gem
Manipulate multiple models on form objects [De-accepts_nested_attributes_for]
Use perltidy on CentOS 8
Use Flutter on Ubuntu
Notes on multiple inheritance
Use mod_auth_cas on CentOS 8
Use bat on Centos.
Use mkdir on ubuntu
Use Corretto 11 on Heroku
Use cpplapack on ubuntu
Use Swift Package on Playground
Use multiple databases with Rails 6.0
Use native code on Android
Use multiple checkboxes in Rails6!
Use PG Backups on Heroku
[Rails] How to use gem "devise"
[Rails] How to use devise (Note)
How to use Bio-Formats on Ubuntu 20.04
Use Docker on your M1 Mac
Preparing to use electron-react-boilerplate on Ubuntu 20.4
Ruby On Rails devise routing conflict
Use the iostat command on CentOS 8
Use completion in Eclipse on mac
Use Docker CE (official) on CentOS 8