LoginSignup
1
2

More than 1 year has passed since last update.

[Rails]deviseの導入手順

Last updated at Posted at 2022-01-12

ユーザー管理機能を簡単に実装できるdeviseというGem
初めて学んだ時は理解できず難しいと感じてたけど、使ってるうちに理解できてきて便利さに感動!
なので備忘録として記事にしときます。

deviseの導入手順

①Gemfileを下記のように編集

Gemfile
#最後の行に追記
gem 'devise'

②下記のコマンドを実行してdeviseをインストール

ターミナル
% bundle install

インストールしたGemの反映するタイミングはサーバー起動時なので、ローカルサーバーを起動しているのであれば再起動

③以下のコマンドを実行して設定ファイルを作成

追加したdeviseの「設定関連に使用するファイル」を自動で生成するコマンド
deviseを追加したらとりあえず1度実行しておく、くらいの認識で

ターミナル
% rails g devise:install

このようなログが流れれば成功!
            create  config/initializers/devise.rb
      create  config/locales/devise.en.yml
===============================================================================

Depending on your application's configuration some manual setup may be required:

  1. Ensure you have defined default url options in your environments files. Here
     is an example of default_url_options appropriate for a development environment
     in config/environments/development.rb:

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

     In production, :host should be set to the actual host of your application.

     * Required for all applications. *

  2. Ensure you have defined root_url to *something* in your config/routes.rb.
     For example:

       root to: "home#index"

     * Not required for API-only Applications *

  3. Ensure you have flash messages in app/views/layouts/application.html.erb.
     For example:

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

     * Not required for API-only Applications *

  4. You can copy Devise views (for customization) to your app by running:

       rails g devise:views

     * Not required *

===============================================================================

④deviseのUserモデル作成

作成には通常のモデルの作成方法(rils g model ○○)ではなく、deviseのモデル作成用コマンドでUserモデルを作成
以下のコマンドで実行

ターミナル
% rails g devise user

このようなログが流れれば成功!
      invoke  active_record
      create    db/migrate/20200309082300_devise_create_users.rb
      create    app/models/user.rb
      invoke    test_unit
      create      test/models/user_test.rb
      create      test/fixtures/users.yml
      insert    app/models/user.rb
       route  devise_for :users

ログを見てみるとユーザーに関するモデルやマイグレーションも自動生成されていることがわかる
また、routes.rbに以下のルーティングが自動的に追記される

config/routes.rb
Rails.application.routes.draw do
   devise_for :users # 追記されている
end

マイグーションファイルを確認してみると以下のようになっている

db/migrate/20XXXXXXXXX_devise_create_users.rb
class DeviseCreateUsers < ActiveRecord::Migration[6.0]
 def change
   create_table :users do |t|
     ## Database authenticatable
     t.string :email,              null: false, default: ""
     t.string :encrypted_password, null: false, default: ""

     ## Recoverable
     t.string   :reset_password_token
     t.datetime :reset_password_sent_at

     ## Rememberable
     t.datetime :remember_created_at

     # 省略

     t.timestamps null: false
   end

   add_index :users, :email,                unique: true
   add_index :users, :reset_password_token, unique: true
   # add_index :users, :confirmation_token,   unique: true
   # add_index :users, :unlock_token,         unique: true
 end
end

デフォルトでメールアドレスやパスワードのカラムを作成する記述があることがわかる
他のカラム(ユーザー名など)を追加したい場合は自身で追記する必要があり、
自身で追加したカラムには別にストロングパラーメーターを使えるようにする記述が必要
 
テーブルの設計が確認できたら、rails db:migrateコマンドでマイグレーションを実行してuserテーブルを作成する。
 
以上がRailsにおけるdevise導入方法です
これだけでユーザー管理機能が実装できるとか便利すぎ!
 

※補足等ありましたらコメントいただけると幸いです

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