ネストした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など使いたい時に是非。