1
2

More than 3 years have passed since last update.

deviseだけで管理者を設定したい

Posted at

はじめに

 現在作成中のアプリでユーザーをuseradminに2つに分けたいと思い、実装してみた。

前提

 userのコントローラーやビューやモデルは作成済み。
 管理者設定のgemは使わず、deviseのみで完結させる。

adminコントローラー生成まで

deviseのconfigをいじる。

config/initializers/devise.rb/247行目
  # ==> Scopes configuration
  # Turn scoped views on. Before rendering "sessions/new", it will first check for
  # "users/sessions/new". It's turned off by default because it's slower if you
  # are using only default views.
  config.scoped_views = true ←コメントアウトを外して、trueにする
config/initializers/devise.rb/255行目
# Set this configuration to false if you want /users/sign_out to sign out
  # only the current scope. By default, Devise signs out all scopes.
  config.sign_out_all_scopes = false ←コメントアウトを外してfalseにする

ターミナルで

rails g devise admin

adminコントローラーを少し編集

app/models/admin.rb
class Admin < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
  devise :database_authenticatable, #:registerable, ←コメントアウトする
         :recoverable, :rememberable, :validatable
end
1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2