0
0

More than 1 year has passed since last update.

ユーザー管理機能(devise)の導入

Posted at

はじめに

deviseを用いたユーザー登録機能の実装順序を忘れないように記録に残す。

devise導入の流れ

1.deviseの導入

下記コードの追記と、コマンドの実行

1.gemfileに追記
gem 'devise'
2.gemインストールのコマンド実行
% bundle install

2.deviseの設定に関するファイルの導入

% rails g devise:install

#以下のようなlogが出れば成功
  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 *

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

3.deviseに関するモデルの作成

# deviseコマンドでUserモデルを作成
% rails g devise user

任意でバリデーションを追記。
パスワードに関しては、キーを:passwordにする必要がある。

4.userテーブルの作成

deviseのマイグレーションファイルにユーザー登録に必要なデータを追記する。
この時、デフォルトでemailとencrypted_passwordに関する設定は既になされているので、下手にいじる必要はない。

rails db:migrateコマンドを忘れない。

5.devise用のviewファイルの作成

# rails g deviseコマンドを実施
% rails g devise:views

registrations(新規登録,sign up)、sessions(ログイン,sign in)に関するフォルダを任意で編集する。
入力項目は、ヘルパーメソッドのform_withを使用する。

<注意>

  • registrations画面について
    入力項目はform_withメソッドで作成するが、
    パスワードの項目は、1回目=:password、確認用=:password_confirmationというキー名でないと、
    usersテーブルのencrypted_passwordカラムにデータは保存されない。

  • form_withで使用するオプションについて
    model: @user(userモデル), url: createアクションに関するpath,
    と指定する。

6.ストロングパラメーターを使えるようにする。

サインアップ時に入力する情報はパラメーターとしてサーバーに送信される。
deviseを使わない通常のリクエストの場合は、コントローラーにストロングパラメーターを記述し、
受け取れるパラメーターを制限していた。

deviseに関しても、同様にストロングパラメーターをコントローラーに記述する。
しかし、deviseの処理を行うコントローラーはGem内に記述されているため、編集することができない。

また、deviseでログイン機能を実装した場合は、
paramsの他に、paramsとは異なる形のパラメーターも受け取っている。

以上から、deviseのコントローラーにストロングパラメーターを反映する方法と、
devise特有のパラメーターを取得する方法が、必要になる。

まずはdevise特有のパラメーターを取得するために、
deviseが提供しているdevise_parameter_sanitizerメソッドを使用する。

# devise_parameter_sanitizerメソッドの利用法
devise_parameter_sanitizer.permit(:deviseの処理名, keys: [:許可するキー,:許可するキー,・・・])

# deviseの処理名の種類
:sign_in	             サインイン(ログイン)の処理を行うとき
:sign_up	             サインアップ(新規登録)の処理を行うとき
:account_update	     アカウント情報更新の処理を行うとき

上述した通り、deviseの処理に関わるコントローラーはGemに記述されており、編集ができない。
そのため、編集ができるapplication_controller.rbにストロングパラメーターを定義しておき、
その処理を読み込ませる。
(コントローラーに定義するnewやcreateなどのアクション内容は、デフォルトで定義されている)

# app/controllers/application_controller.rb内に記述
class ApplicationController < ActionController::Base
  before_action :configure_permitted_parameters, if: :devise_controller?

  private
  def configure_permitted_parameters  #メソッド名は慣例
    devise_parameter_sanitizer.permit(:sign_up, keys: [:nickname])
  end
end

<補足>
before_actionのifというオプションを使用していますが、
これは、値にメソッド名を指定することで、その戻り値がtrueであったときにのみ処理を実行するよう設定するものです。

今回は:devise_controller?というdeviseのヘルパーメソッド名を指定して、
もしdeviseに関するコントローラーの処理であれば、そのときだけconfigure_permitted_parametersメソッドを実行するように設定している。
他のコントローラーなどでは処理は読み込まれても、実行まではされない。
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