0
0

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.

ストロングパラメーターとViewのform_withに関係に着目

Last updated at Posted at 2021-04-25

現在、FormObjectに取り組んでいます。

自分がエラーから以下のことがわかりました。

環境

①addressテーブル ③(FormObject)address_purchase.rb ④(View)purchasesフォルダindex.html.erb
②purchaseテーブル 上記 上記

④のViewのフォームから来たデータを①テーブルと②版テーブルに分けて保存することを目指しています。

問題:1つだけ変数を受け取れない場合があります。

1:form_with model:@インスタンス変数
2:form_with url ✖️_✖️_path
3:form_with model:@インスタンス変数, url ✖️_✖️_path

1番が受け取れない場合があります。

address_purchase.rb
  private

  def address_params
    params.require(:address_purchase).permit(:postal_code, :prefecture_id, :town, :address, :building, :phone_number).merge(user_id: current_user.id, item_id: params[:item_id], purchase_id:params[:id])
  end

上に対応するviewの書き方が以下

app/views/purchases/index.html.erb
<%= form_with model: @address_purchase,url: item_purchases_path, id: 'charge-form', class: 'transaction-form-wrap',local: true do |f| %>

###A:require(:adress_purchase)部分が model: @address_purchase部分と対応してます

address_purchase.rb
  private

  def address_params
    params.permit(:postal_code, :prefecture_id, :town, :address, :building, :phone_number).merge(user_id: current_user.id, item_id: params[:item_id], purchase_id:params[:id])
  end

上に対応するviewの書き方が以下

app/views/purchases/index.html.erb
<%= form_with url: item_purchases_path, id: 'charge-form', class: 'transaction-form-wrap',local: true do |f| %>

###B:permit( )部分が model: url: item_purchases_path部分と対応してます

address_purchase.rb
  private

  def address_params
    params.require(:address_purchase).permit(:postal_code, :prefecture_id, :town, :address, :building, :phone_number).merge(user_id: current_user.id, item_id: params[:item_id], purchase_id:params[:id])
  end

上に対応するviewの書き方が以下

app/views/purchases/index.html.erb
<%= form_with url: item_purchases_path, id: 'charge-form', class: 'transaction-form-wrap',local: true do |f| %>

###C:require(:address_purchase)が model: @address_purchase部分がないためにエラーが出ます。

Cの場合のエラーの確認方法

エラーメッセージ画像
20210425-175817.png

取れるパワーメータも{}1重カッコのハッシュ。

{"authenticity_token"=>"rSIr68JcqA8eDoKKyZJKqw6boN9tEiBhVKCu2k1vhvW08xn+gVor7jmy+pCWqWkTXLydOrrP/MU0IpDpwJG18Q==",
"card_number"=>"4242424242424242",
"month"=>"4",
"year"=>"35",
"cvc"=>"123",
"postal_code"=>"123-4567",
"prefecture_id"=>"3",
"town"=>"あわわわ",
"address"=>"あわ1",
"building"=>"",
"phone_number"=>"09077778888",
"commit"=>"購入",
"item_id"=>"24"}

違う形に直すと

取れるパワーメータも{ }2重カッコのハッシュ。これが正解

Parameters: {"authenticity_token"=>"UjD+CkBD6QzxFEd1vJgxgQFKE0ifhoSE0GUnUrdwMSFL4cwfA0Vq7daoP2/joxI5U20urUhbWCCw5xlhOo4CJQ==", "address_purchase"=>{"card_number"=>"4242424242424242", "month"=>"3", "year"=>"34", "cvc"=>"123", "postal_code"=>"123-4567", "prefecture_id"=>"2", "town"=>"あああ", "address"=>"あああ", "building"=>"", "phone_number"=>"09011112222"}, "commit"=>"購入", "item_id"=>"24"}

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?