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

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

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

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

Ruby on Rails

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

Q&A

解決済

1回答

767閲覧

editアクションでparamsの配列の中身が取得できない。

show_kanazawa

総合スコア12

Ruby

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

Ruby on Rails

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

0グッド

0クリップ

投稿2019/11/24 03:55

商品出品ができるサイトの編集ページ(editアクション)を実装中のところです。
商品(productモデル)に対して複数画像 (imagesモデル)を登録できるようにしておりimagesモデルをネストしております。
編集ページにアクセスした際、formで登録ずみの画像を表示させており、保存されている画像が表示されるのですが、画像を変更してもしなくても、更新ボタンを押すとimage_urlに値が入っておらず下記エラーが表示されてしまいます。
ActiveRecord::NotNullViolation in ProductsController#update
エラー画面
https://gyazo.com/19831190041103672cdd7c656d281304

画像のアップロードにはcarrierwaveを使っています

おそらくコントローラのストロングパラメーターにてimageの部分を正しく指定ができていないので指定方法のアドバイスをご教授いただきたいです。

edit.html.haml

1 2.new-conteiner 3 = render 'shared/new_header' 4 .new-main 5 .new-main__box 6 %h2 商品の情報を入力 7 = form_for @product do |f| 8 .image 9 %h4 出品画像 10 .image__images 11 12 13 = f.fields_for :images do |image| 14 .image__images__box{"data-image-id": "#{image.object.id}"} 15 = image.file_field :image_url, class: "file-field", style: "display:none;", id: "edit-file", value: "#{image.object.image_url.url}" 16 = image_tag(image.object.image_url.url, id: "product_image", height: '120', width: '120') 17 = image.hidden_field :id, value: image.object.id 18 .edit_btns 19 .change_btn 20 .change_btn-text 編集 21 .delete_btn 22 .delete_btn-text 削除

product.controller.rb

1 2def edit 3 @product = Product.find(params[:id]) 4 @user = current_user 5 @images = Image.where(product_id: @product) 6 @category_parent_array = ["---"] 7 Category.where(ancestry: nil).each do |parent| 8 @category_parent_array << parent.name 9 end 10 end 11 12def update 13 if @product.update(product_params) 14 redirect_to product_path 15 else 16 render 'buyer_show' 17 end 18 end 19 20private 21 def product_params 22 params.require(:product).permit( 23 :title, :image, :text, :price, :saler_id, {categories: []}, 24 images_attributes: [:image_url, :id], -#この部分の指定が正しくできていないのかと思われます 25 condition_attributes: [:condition], 26 freight_attributes: [:freight], 27 root_area_attributes: [:root_area], 28 day_attributes: [:day] 29

product.rb

1 2class Product < ApplicationRecord 3 4 5 has_many :images, dependent: :destroy 6 accepts_nested_attributes_for :images 7 8 9end 10

image.rb

1 2class Image < ApplicationRecord 3 belongs_to :product, inverse_of: :images 4 mount_uploader :image_url, ImageUploader 5end

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

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

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

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

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

guest

回答1

0

自己解決

viewに
= image.hidden_field :image_url, value: image.object.image_url
を書いてしまっていたのですが消すことで保存できました!

投稿2019/11/24 04:45

show_kanazawa

総合スコア12

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問