0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【Rails】devise1つで複数モデルを管理する方法

Last updated at Posted at 2020-04-27

開発環境

・Ruby: 2.5.7
・Rails: 5.2.4
・Vagrant: 2.2.7
・VirtualBox: 6.1
・OS: macOS Catalina

#1.deviseを導入

Gemfile
gem 'devise'
ターミナル
$ bundle
ターミナル
$ rails g devise:install

#2.「devise.rb」を編集

config/devise.rb
# 235行目をコメントアウトして、「true」に変更
config.scoped_views = true

#3.モデルを作成

ターミナル
$ rails g devise user
ターミナル
$ rails g devise admin

#4.コントローラーを作成

ターミナル
$ rails g devise:controllers users
ターミナル
$ rails g devise:controllers admins

#4.ビューを作成

ターミナル
$ rails g devise:views users
ターミナル
$ rails g devise:views admins

#5.ルーティングを編集

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

    devise_for :admins, controllers: {
      sessions:      'admins/sessions',
      passwords:     'admins/passwords',
      registrations: 'admins/registrations'
    }
end

#6.必要無いビューを削除する

ターミナル
$ rails d devise:views
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?