23
27

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 5 years have passed since last update.

RailsのDeviseで新たに追加したカラムをStrong Parametersに追加してSignup/Updateする方法

Posted at

Deviseは本当に便利で、ユーザー作成・認証・更新周りの開発が、慣れればサクサク進むなーという印象なのですが、いかんせん私みたいな初心者は、裏側でどのように動いているかが見えないので、なぜこれで動くの?と疑問に思うシーンが多々発生します。

なので、開発を進める中で気になったところを、メモ的に残します。
なによりも、公式ドキュメントをちゃんと読むことが大事。
https://github.com/plataformatec/devise

RailsのDeviseで新たに追加したカラムをStrong Parametersに追加してSignup/Updateする方法

Strong Parametersで追加したいカラムをこのように追加。

registrations_controller.rb
  # protected

  # If you have extra params to permit, append them to the sanitizer.
  # def configure_sign_up_params
  #   devise_parameter_sanitizer.permit(:sign_up, keys: [:attribute])
  # end

  # If you have extra params to permit, append them to the sanitizer.
   def configure_account_update_params
     devise_parameter_sanitizer.permit(:account_update, keys: [:name]) //追加した
     devise_parameter_sanitizer.permit(:account_update, keys: [:description]) //追加した 
     devise_parameter_sanitizer.permit(:account_update, keys: [:attribute])
   end

そして忘れがちなのが、before_actionで有効化すること。

registrations_controller.rb
class Users::RegistrationsController < Devise::RegistrationsController
  # before_action :configure_sign_up_params, only: [:create]
  before_action :configure_account_update_params, only: [:update] //ここの話

これでちゃんと動いた。

23
27
1

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
23
27

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?