LoginSignup
6
6

More than 3 years have passed since last update.

images_attributesのMysql2::Error: Column 'image_url' cannot be null: INSERT INTO `地獄エラーの解決策【3文字で解決した】

Posted at

 はじめに

3日間1つのエラーに苦しみ「調べても調べても調べても」解決できずに時間を削ってしまったので
もし同じような境遇の方に助けになればと思い書かせていだだきます。
(3日間も苦しんだので書かずにはいられない・・・笑)

こんな感じで画像編集しようとした

edit.rb
- @product.images.each_with_index do |image, i|
  %label{class: "sell__image-select__ul-text ul-id-#{i}"}
    = f.fields_for :images, image do |img|
      = img.file_field :image_url,"data-id": "#{i}",class: "file_field"
      %li
        -if image.id.present?
          = image_tag "#{img.object.image_url}",size: "114x116",class:"product_image"
      .btn-box
        %a 編集
        %a{ class: "delete-btn", "data-imgid": "#{image.id}" }削除

・ここの解説

@productにはDBから引っ張ってきた商品データが入っていて

= image_tag "#{img.object.image_url}",size: "114x116",class:"product_image"

ここで商品を表示させてる

pudateアクションを発動させる

・エラー文

スクリーンショット 2019-06-16 13.05.54.png

・paramsの中

スクリーンショット 2019-06-16 13.04.17.png

んーー確かにimage_urlはないな・・・

・ストロングパラメーターはこちら(一部省略)

products_controller.rb
private
def update_params
params.require(:product).permit(:name,images_attributes: [:image_url]).merge(seller_id: current_user.id)
end

ならばimage_urlを入れてみよう

edit.rb
- @product.images.each_with_index do |image, i|
  %label{class: "sell__image-select__ul-text ul-id-#{i}"}
    = f.fields_for :images, image do |img|
      = img.file_field :image_url,"data-id": "#{i}",class: "file_field"
      %li
        -if image.id.present?
          = image_tag "#{img.object.image_url}",size: "114x116",class:"product_image"
          = img.hidden_field :image_url, val: img.object
      .btn-box
        %a 編集
        %a{ class: "delete-btn", "data-imgid": "#{image.id}" }削除

・出来上がったparamsはこちら

スクリーンショット 2019-06-16 13.06.35.png

見にくいですけどimage_urlは入ってますね
ならばアップデートアクションを動かしてみよう。

結果・・・

スクリーンショット 2019-06-16 13.07.02.png
どぉぉぉぉしてだよぉぉぉ〜

エラーの言う通りimage_urlは入れたし
何がいけないんだ・・・TT

ここで数日間止まりました。。

解決策

products_controller.rb
private
def update_params
params.require(:product).permit(:name,images_attributes: [:image_url,:id]).merge(seller_id: current_user.id)
                                                                    #:idを追加
end

これだけです。。
なので

edit.rb
- @product.images.each_with_index do |image, i|
  %label{class: "sell__image-select__ul-text ul-id-#{i}"}
    = f.fields_for :images, image do |img|
      = img.file_field :image_url,"data-id": "#{i}",class: "file_field"
      %li
        -if image.id.present?
          = image_tag "#{img.object.image_url}",size:"114x116",class:"product_image"
          #= img.hidden_field :image_url, val: img.object
          #これは必要ありません
      .btn-box
        %a 編集
        %a{ class: "delete-btn", "data-imgid": "#{image.id}" }削除

paramsは今まで通り
image_urlはない状態
スクリーンショット 2019-06-16 13.08.13.png
これで通ります・・・
画像編集時には

images_attributes: [:image_url,:id]

idがいるんですね。
(どの画像を編集するのだ?)と言うことだったんですね・・・

むちゃくちゃ強引にアレンジしてすればもしかしたら通すことができたかもしれませんが
しっかり戦ってよかったなぁと思いました。。

このエラーに3日も使ってしまったので
もお同じことを繰り返すまいとおQiitaに保存します。

最後まで読んでいただきありがとうございました。

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