39
62

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.

deviseでストロングパラメータを追加する (Rails5を受けての変更点)

Last updated at Posted at 2017-02-27

はじめに

devise3からdevise4へのアップデートに伴い、ストロングパラメータの追加の仕方が変わっていたのでメモしておきます。

Remove ParametersSanitizer inheritance.

devise_parameter_sanitizer.for から devise_parameter_sanitizer.permit に変わったようです。

forのままだとこんな感じのエラーになりました。

NoMethodError (undefined method 'for' for # Did you mean? fork)

forよりもpermitのほうがわかりやすいと個人的に思ったのでなかなかよいのではないでしょうか。

具体例

devise3

app/controllers/application_controller.rb
  before_action :configure_permitted_parameters, if: :devise_controller?

  protected

    def configure_permitted_parameters
      devise_parameter_sanitizer.for(:sign_up) << :username
      devise_parameter_sanitizer.for(:sign_in) << :username
      devise_parameter_sanitizer.for(:account_update) << :username
    end

devise4

app/controllers/application_controller.rb
  before_action :configure_permitted_parameters, if: :devise_controller?

  protected

  def configure_permitted_parameters
    devise_parameter_sanitizer.permit(:sign_up, keys: [:username])
    devise_parameter_sanitizer.permit(:sign_in, keys: [:username])
    devise_parameter_sanitizer.permit(:account_update, keys: [:username])
  end
39
62
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
39
62

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?