LoginSignup
0
2

More than 1 year has passed since last update.

【Rails6】deviseで独自カラムを追加して使用する方法 ③

Last updated at Posted at 2021-05-10

これまで

これまでに、deviseのインストール・Userモデルの作成・Viewの作成を行いました。

今回は、コントローラ・ルーティング・モデルのカスタマイズを行っていきたいと思います。

▽前回の記事はこちら▽
【Rails6】deviseで独自カラムを追加して使用する方法 ①

【Rails6】deviseで独自カラムを追加して使用する方法 ②

① Controller 作成

View を追加するrails g devise:controllers users コマンドを実行

$ rails g devise:controllers users
Running via Spring preloader in process 5360
      create  app/controllers/users/confirmations_controller.rb #認証メール再送用
      create  app/controllers/users/passwords_controller.rb #パスワード変更関連用
      create  app/controllers/users/registrations_controller.rb #ユーザ登録関連用
      create  app/controllers/users/sessions_controller.rb #ログイン用
      create  app/controllers/users/unlocks_controller.rb #ロック解除メール再送用
      create  app/controllers/users/omniauth_callbacks_controller.rb #外部ログイン用
===============================================================================

Some setup you must do manually if you haven't yet:

  Ensure you have overridden routes for generated controllers in your routes.rb.
  For example:

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

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

上記コマンドで、ひと通り devise 用の Controller ( 各ファイルに # でファイルの用途を記載しました ) が生成されます。

上記のコマンド結果に記載の通り、カスタマイズしたコントローラを反映させるためには、ルーティング設定をしないといけません。

② routes.rb 編集

/config/routes.rb ファイルを編集していきます。

routes.rb_編集前
Rails.application.routes.draw do
  devise_for :users
  # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
end
routes.rb_編集後
Rails.application.routes.draw do

  devise_for :users, controllers: {
    confirmations: 'users/confirmations',
    passwords: 'users/passwords',
    registrations: 'users/registrations',
    sessions: 'users/sessions',
    unlocks: 'users/unlocks'
  }

  # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
end

上記のように、カスタマイズするコントローラを記載していきます。

③ コントローラのカスタマイズ

今回は、独自カラムを追加していますが、特段コントローラーを変更する必要はありませんが、
今回Devise用の レイアウトテンプレート を使いページを表示させるため、コントローラへレイアウトテンプレートを読み込ませる文を追記します。
また、 レイアウトテンプレートのページタイトルを <title><%= @page_title%> | Sample</title としてインスタンス変数を入れページ毎のページタイトルに対応させているので、コントローラ へ ページタイトルも追記します。

同じ作業になるので、例で /users/confirmations_controller.rb に追記します。

confirmations_controller.rb_編集前
# frozen_string_literal: true

class Users::ConfirmationsController < Devise::ConfirmationsController
  # GET /resource/confirmation/new
  # def new
  #   super
  # end

  # POST /resource/confirmation
  # def create
  #   super
  # end

  # GET /resource/confirmation?confirmation_token=abcdef
  # def show
  #   super
  # end

  # protected

  # The path used after resending confirmation instructions.
  # def after_resending_confirmation_instructions_path_for(resource_name)
  #   super(resource_name)
  # end

  # The path used after confirmation.
  # def after_confirmation_path_for(resource_name, resource)
  #   super(resource_name, resource)
  # end
end
confirmations_controller.rb_編集後
# frozen_string_literal: true

class Users::ConfirmationsController < Devise::ConfirmationsController

  layout "devise"

  # GET /resource/confirmation/new
  def new
    @page_title = "確認メールが届かない場合"
    super
  end

  # POST /resource/confirmation
  # def create
  #   super
  # end

  # GET /resource/confirmation?confirmation_token=abcdef
  # def show
  #   super
  # end

  # protected

  # The path used after resending confirmation instructions.
  # def after_resending_confirmation_instructions_path_for(resource_name)
  #   super(resource_name)
  # end

  # The path used after confirmation.
  # def after_confirmation_path_for(resource_name, resource)
  #   super(resource_name, resource)
  # end
end

layout "login"とコントローラ内に記載すると、Rails側で devise.html.erbをレイアウトテンプレートとして認識します。

④ モデルのカスタマイズ

モデルへは、Deviseで使用する機能を追記します。

user.rb_編集前
class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable
end
user.rb_編集後
class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable,
         :trackable, :confirmable, :lockable, :timeoutable
end
No 機能 説明
1 database_authenticatable デフォルト
2 registerable デフォルト
3 recoverable デフォルト
4 rememberable デフォルト
5 validatable デフォルト
6 trackable ログイン回数・ログイン日時・IPアドレスの記録
7 confirmable 認証メールの送信・メール認証の確認
8 lockable ログインの連続失敗によるロック・ロック解除メール送信 
9 timeoutable 一定期間動作のないアカウントのログアウト
10 omniauthable 外部サービスの認証 (Twitter・Facebookなど)

上記の機能の説明を表にしてみました。

ここまで行うと、使用する機能・コントローラ・レイアウトテンプレートが反映されます。

長くなりましたので、記事を分けてご紹介しようと思います。
次回は、デバイスの設定 ( 独自カラムでログインできるようにする ) 行っていきたいと思います。

▽次の記事はこちら▽
【Rails6】deviseで独自カラムを追加して使用する方法 ④

0
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
0
2