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.

登録対象が複数必要な場合のdevise導入について

Posted at

この記事において紹介する内容

この記事はdeviseを用いて複数のテーブルにデータを保存する方法を紹介するものであります。修正点等あれば教えていただけますと幸いです。
rails (6.0.3.5)
ruby (2.6.5)

gemfileへのの記述

・railsのgemfilenにgem 'devise'と入力する。

deviseの導入

bundle installを実行した後、rails g devise:installを実行。ここからはコードエディタをしていきます。

コードエディタの記述

config/initializers/devise.rbに手を加えます。

config.scoped_views = true

上記の用に記述する。
(ここをtrueにすることでviewsが編集可能となります。)

モデル/コントローラー/ビューファイルの作成

・こちらに関しては

rails g devise "モデル名"
rails g devise controllers "コントローラー名"
rails g devise views "モデル名"

上記コマンドで作成します。

ルーティングの設定

ここに修正を加えていきます。現時点でrails routesを使用してターミナルを見てみると、重複したルーティングを見つけることができると思います。
 ex)userとadminの2者でdeviseを実装しようとした場合を考えていきます。

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

一言

何気に初めての事でしたので備忘録も兼ねて本記事を作成しました。

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?