LoginSignup
2
0

More than 5 years have passed since last update.

StrongParameterに#to_hashしたら警告が出たよ

Last updated at Posted at 2018-02-20

ネストしたStrongParameterを整形したかったので params[:hoge].to_hashしたらなんか警告が出ました

環境

  • OSX
  • Ruby 2.3.1
  • Rails 5.0.4
  • puma 3.9.1

警告文曰く


pry > params[:hoge].to_hash
DEPRECATION WARNING: #to_hash unexpectedly ignores parameter filtering, and will change to enforce it in Rails 5.1. Enable `raise_on_unfiltered_parameters` to respect parameter filtering, which is the default in new applications. For the existing deprecated behaviour, call #to_unsafe_h instead. (called from map at (pry):11)

あーね。
StrongParameterのフィルタリングをシカトしてる推奨されないやり方だから別メソッドで対応しなさいと。
Rails5.1だと別メソッドが提供されてるからそっち使ってちょということでした


pry > params[:hoge].to_unsafe_h

上記のto_unsafe_hメソッドで解決
まあ警告文みたらわかるけど、備忘録としてメモ。

追記2019/3/19

Rails5.2からStrongParameterに対してto_hashを行うとデフォルトでエラーになるようになりました。

そのため、以下の手続きが必要です。

# permitしたパラメータに対してto_hashを行う
params.permit(:hoge).to_hash

paramsに対してmapなど使いたい時に是非。

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