質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Q&A

解決済

1回答

838閲覧

商品編集の際にUPDATEアクションが行えない

chanken2314

総合スコア23

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

0グッド

0クリップ

投稿2020/01/27 12:32

前提・実現したいこと

商品編集の際にUPDATEアクションを行いたい

商品の出品はできている。

発生している問題・エラーメッセージ

エラーGIF
https://gyazo.com/4dc564d739cad8f4ac8a0123371860bd

該当のソースコード

itemcont

1 def edit 2 @item = Item.find(params[:id]) 3 @images = @item.images 4 end 5 6 def update 7 @item = Item.find(params[:id]) 8 @item.update(item_params) 9 end 10 11 private 12 def item_params 13 params.require(:item).permit( :name, :description, :category_id, :size_id, :brand_id, :prefecture_id, :condition_id, :delivery_charge_id, :delivery_way_id, :delivery_days_id, :price,images_attributes: [:image_url]) 14 end

edit

1.sell_item_html 2 = render "/items/sheard/icon_header" 3 4 // 商品フォームヘッダー 5 .sell_form 6 .sell_form__main 7 %h2.sell_form__main__head 8 商品の情報を入力 9 10 .sell-main__form 11 = form_for(@item, url: item_path(@item), html: {method: "patch"}) do |f| 12 // 画像アップロード 13 .sell_form__main__box 14 .sell_upload_box 15 16 %h3.sell_upload_box__head 17 = f.label :image, "出品画像" 18 %span.form-require 19 必須 20 %p.info 最大10枚までアップロードできます 21 .sell_dropbox 22 ドラッグアンドドロップ 23 %br 24 またはクリックしてファイルをアップロード 25 .image-box__container 26 #image-box-1 27 .item-num-0#image-box__container 28 = f.fields_for :images do |i| 29 .input-area 30 = i.file_field :images,type: 'file', name: "item[images_attributes][][image_url]", value:"image_url", style: "display:none", id:"img-file",required: true 31 %label.input{for: "img-file",value: "patch"} 32 %i.fas.fa-camera 33 34 35 36 // 商品名入力 37 .sell_content 38 .sell_content_form_group 39 %label.sell_name 40 = f.label :name do 41 商品名 42 %span.form-require 43 必須 44 = f.text_field :name, required: true, class: "sell_form_name", placeholder: "商品名(必須40文字まで)" 45 .sell_content_item_group 46 %label.sell_item_description 47 = f.label :description do 48 商品の説明 49 %span.form-require 50 必須 51 = f.text_area :description,required: true, class: "sell_item_textarea", placeholder: "商品の説明(必須 1,000文字以内)(色、素材、重さ、定価、注意点など)例)2010年頃に1万円で購入したジャケットです。ライトグレーで傷はありません。あわせやすいのでおすすめです。" 52 53 // 商品詳細 54 .sell_detail_content 55 .sell_detail_content__head 56 商品の詳細 57 .sell_detail_content__box 58 .sell_detail_content__box__form 59 60 -# 商品カテゴリー 61 .listing-form-box 62 .listing-product-detail__category 63 = f.label :category_id do 64 カテゴリー 65 %span.form-require 必須 66 .listing-select-wrapper 67 .listing-select-wrapper__box 68 = f.select :category_id,@category_parent_array, {}, {class: 'listing-select-wrapper__box--select', required: true, id: 'parent_category'} 69 %i.fas.fa-chevron-down.listing-select-wrapper__box--arrow-down 70 71 72 .sell_item_detail_box_form_group 73 %label.item_status 74 = f.label :size_id, "サイズ" 75 %span.form-require 76 任意 77 .category_select_wrap 78 %i.icon_arrow_bottom 79 = f.select :size_id, Item.sizes.keys,{prompt: '---'} 80 81 .sell_item_detail_box_form_group 82 %label.item_status 83 = f.label :brand_id, "ブランド" 84 %span.form-require 85 任意 86 .category_select_wrap 87 %i.icon_arrow_bottom 88 = f.select :brand_id, Item.brands.keys,{prompt: '---'} 89 90 -# 商品ステータス 91 .sell_item_detail_box_form_group 92 %label.item_status 93 = f.label :condition_id, "商品の状態" 94 %span.form-require 95 必須 96 .category_select_wrap 97 %i.icon_arrow_bottom 98 = f.select :condition_id, Item.conditions.keys,{prompt: '---'},required: true 99 100 -# 配送について 101 .shipping_item_content 102 .shipping_item_content__head 103 配送について 104 .shipping_item_content__box 105 .shipping_item_content__box__form 106 107 .shipping_item_content__box__form_group 108 %label.shipping_charge 109 = f.label :delivery_charge_id, "配送料の負担" 110 %span.form-require 111 必須 112 .category_select_wrap 113 %i.icon_arrow_bottom 114 = f.select :delivery_charge_id, Item.delivery_charges.keys,{prompt: '---'},required: true 115 116 .shipping_item_content__box__form_group 117 %label.shipping_charge 118 = f.label :prefecture_id, "配送元地域" 119 %span.form-require 120 必須 121 .category_select_wrap 122 %i.icon_arrow_bottom 123 = f.select :prefecture_id, Item.prefectures.keys,{prompt: '---'},required: true 124 125 .shipping_item_content__box__form_group 126 %label.delivery_way_id 127 = f.label :delivery_way_id, "配送の方法" 128 %span.form-require 129 必須 130 .category_select_wrap 131 %i.icon_arrow_bottom 132 = f.select :delivery_way_id, Item.delivery_ways.keys,{prompt: '---'},required: true 133 134 .shipping_item_content__box__form_group 135 %label.shipping_charge 136 = f.label :delivery_days_id, "発送までの日数" 137 %span.form-require 138 必須 139 .category_select_wrap 140 %i.icon_arrow_bottom 141 = f.select :delivery_days_id,Item.delivery_days.keys,{prompt: '---'},required: true 142 143 .item_price_content 144 .item_price_content__head 145 販売価格(300〜9,999,999) 146 147 .item_price_content__box 148 .item_price_content__box__form 149 %label.item_price 150 = f.label :price, "価格" 151 %span.form-require 152 必須 153 .item_price_input 154 %p.price¥ 155 .input_price 156 %input#price_calc.price_form-box{placeholder: "例) 300", type: "number"} 157 158 .item_handling_charge 159 販売手数料 (10%) 160 .right_bar 161 162 .sell_profit 163 .sell_profit__left 164 販売利益 165 = f.number_field :price, style: "display: none", id: "price" 166 .right_bar_2 167 168 .sell_btn_box 169 .caution 170 %p.caution 禁止されている出品,行為を必ずご確認ください。 171 %p.caution またブランド品でシリアルナンバー等がある場合はご記載ください。 172 %p.caution 偽ブランドの販売,は犯罪であり処罰される可能性があります。 173 %p.caution また、出品をもちまして,加盟店規約,に同意したことになります。 174 175 .sell_btn 176 = f.submit "出品する", class: 'sell__about__bottom-btn submit' 177 .back_btn 178 戻る 179 180 = render "/items/sheard/icon_footer" 181

仮説

フォーム送信時にpatchになっていない

試したこと

binding.pryをアクションのところにかけてみたがスルーされる

補足情報(FW/ツールのバージョンなど)

rails.5.0.7

よろしくお願いします

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

bamboo-nova

2020/01/27 12:41

実際のエラー文がないと何処が原因が推定するのが難しいので、エラー文を追記した方が良いかと思います。
chanken2314

2020/01/27 12:50

ご指摘ありがとうございます。 エラー自体がコンソールログの POST http://localhost:3000/items/1 400 (Bad Request)となります。 この時点でPOSTになっているのでリクエストがおかしいエラーだと推測しています。
winterboum

2020/01/27 13:04

エラーはそこだけじゃなくて、全文を載せてください。 画像じゃなくてtextのコピペで
chanken2314

2020/01/27 13:10

A cookie associated with a cross-site resource at <URL> was set without the `SameSite` attribute. A future release of Chrome will only deliver cookies with cross-site requests if they are set with `SameSite=None` and `Secure`. You can review cookies in developer tools under Application>Storage>Cookies and see more details at <URL> and <URL>. Navigated to http://localhost:3000/items/1 Navigated to chrome-error://chromewebdata/ Navigated to http://localhost:3000/items/1/edit edit:1 A cookie associated with a cross-site resource at http://google.com/ was set without the `SameSite` attribute. A future release of Chrome will only deliver cookies with cross-site requests if they are set with `SameSite=None` and `Secure`. You can review cookies in developer tools under Application>Storage>Cookies and see more details at https://www.chromestatus.com/feature/5088147346030592 and https://www.chromestatus.com/feature/5633521622188032. edit:1 A cookie associated with a cross-site resource at https://accounts.google.com/ was set without the `SameSite` attribute. A future release of Chrome will only deliver cookies with cross-site requests if they are set with `SameSite=None` and `Secure`. You can review cookies in developer tools under Application>Storage>Cookies and see more details at https://www.chromestatus.com/feature/5088147346030592 and https://www.chromestatus.com/feature/5633521622188032. edit:1 A cookie associated with a cross-site resource at https://ogs.google.com/ was set without the `SameSite` attribute. A future release of Chrome will only deliver cookies with cross-site requests if they are set with `SameSite=None` and `Secure`. You can review cookies in developer tools under Application>Storage>Cookies and see more details at https://www.chromestatus.com/feature/5088147346030592 and https://www.chromestatus.com/feature/5633521622188032. localhost:3000/items/1/edit:1 A cookie associated with a cross-site resource at https://google.com/ was set without the `SameSite` attribute. A future release of Chrome will only deliver cookies with cross-site requests if they are set with `SameSite=None` and `Secure`. You can review cookies in developer tools under Application>Storage>Cookies and see more details at https://www.chromestatus.com/feature/5088147346030592 and https://www.chromestatus.com/feature/5633521622188032. localhost:3000/items/1/edit:1 A cookie associated with a cross-site resource at http://www.google.com/ was set without the `SameSite` attribute. A future release of Chrome will only deliver cookies with cross-site requests if they are set with `SameSite=None` and `Secure`. You can review cookies in developer tools under Application>Storage>Cookies and see more details at https://www.chromestatus.com/feature/5088147346030592 and https://www.chromestatus.com/feature/5633521622188032.
guest

回答1

0

ベストアンサー

images_attributesの部分は、「編集」時の際にはストロングパラメータの部分で

ruby

1images_attributes: [:image_url, :id]

のようにする必要があります。これは、どの画像を編集すれば良いのか分からないのでidが必要なためです。おそらく、これでパラメータが足りなくて400のエラーになったんだと思います。(念の為、ちゃんとエラーの本文を確認してみてください)

以下の参考資料に載っていました。
https://qiita.com/Masataka_Sugi/items/11ff9f228d347b5e5b89

投稿2020/01/27 13:09

bamboo-nova

総合スコア1408

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

chanken2314

2020/01/27 13:22

エラー解決しました! ほんとにすいません。至らない情報で申し訳ありませんでした。 とても参考になりました。 ストロングパラメータの設定と、editのview修正でいけました。 自分なりにまとめて、qiitaに投稿したいと思います。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問