4
2

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.

【大枠】paramsでハッシュの中のハッシュから値を取得するお話

Last updated at Posted at 2016-10-22

#こんなことありませんか?
フォームから送られてきたパラメーターをparamsメソッドを使って取得し値をデータベースに保存しようとしたが一部ハッシュが二重構造になっている・・・・・・。

【パラメーター】
=> {"utf8"=>"✓", "name"=>"takeshi", "age"=>"22", "IT"=>{"Engineer"=>"front"}}

このような場合は、
params.permit(:name, :age).merge(job: params[:IT][:Engineer])と書くことで解決。
paramsメソッドはキーを続けて記述することでハッシュの中にハッシュがある場合でも値を取り出すことができる。

4
2
3

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?