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

requireの使い方

Last updated at Posted at 2021-04-16

#requireメソッド
送信されたパラメーターの情報を持つparamsが、使用できるメソッド。
requireメソッドは、パラメーターからどの情報を取得するか、選択する。

params.require(:モデル名)

requireを使うのはパラメーターが二重ハッシュの時のみ。

[1] pry(#<OrdersController>)> params
=> <ActionController::Parameters {"authenticity_token"=>"dIFiIxMsjPdq0lATAzwoZgYl/LKtBrD1Zsoo
gqyCDoVhXGqCAQHoVSWIkvkMhuXn+ooywt50eQJiCfcCniJamQ==",
 "hoge"=>"", "post_code"=>"111-1111", "shipping_area_id"=>"3", "municipality"=>"aa",
 "address"=>"aa", "building_name"=>"", 
"phone_number"=>"00000000000", "commit"=>"購入", "controller"=>"orders", 
"action"=>"create", "item_id"=>"2"} permitted: false>

この場合だとハッシュは一つなのでrequireは使えない。

[1] pry(#<OrdersController>)> params
=> <ActionController::Parameters {"authenticity_token"=>"dIFiIxMsjPdq0lATAzwoZgYl/LKtBrD1Zso
ogqyCDoVhXGqCAQHoVSWIkvkMhuXn+ooywt50e
QJiCfcCniJamQ==","user_purchases"=>{
 "hoge"=>"", "post_code"=>"111-1111", "shipping_area_id"=>"3", 
"municipality"=>"aa", "address"=>"aa", "building_name"=>"", 
"phone_number"=>"00000000000"}, "commit"=>"購入", 
"controller"=>"orders", 
"action"=>"create", "item_id"=>"2"} permitted: false>

このような場合だと二重ハッシュなのでrequireを使うことになる。

二重ハッシュになる場合はモデルを指定した時に生成される。

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