LoginSignup
0
0

More than 3 years have passed since last update.

Railsでdeviseを導入した際にストロングパラメータを設定する

Posted at

devise導入時にデフォルト以外のカラムをDBに追加する

deviseはあらかじめDBに保存できるカラムが決められている。
他のカラム情報をDBに保存する場合は、ストロングパラメーターの設定が別途必要になる。
その設定方法。

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: [:name]) ← 追加したいキー
  end
end

application_controllerにbefore_actionに記述しているため、全てのアクションが実行される前に、 before_action :configure_permitted_parameters, if: :devise_controller?が実行される。
deviseのコントローラーから呼び出された場合は、configure_permitted_parametersメソッドが呼ばれる。

deviseではデフォルトでdevise_parameter_sanitizerメソッドが使える。deviseでユーザー登録をする場合に「特定のカラムを許容する」メソッド。
例として「nameカラム」を新たに追加する場合はこのメソッドを使用し、「name」キーの内容の保存を許可している。

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