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

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

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

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

Q&A

解決済

1回答

597閲覧

railsでfields_forを使い画像データのみ別のテーブルに保存したい

akito_tl

総合スコア2

Ruby on Rails

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

0グッド

0クリップ

投稿2020/09/03 11:57

編集2020/09/03 13:53

#質問
form_withを使ってitemテーブルにデータを保存し、
画像のみitem_imageテーブルへ保存したいと考えています。
その際、fields_forを使用しているのですが、送ることが出来ず
エラー表示もされないため原因も掴めません。(itemテーブルへ他データの保存は行えます)
下記に当該箇所コードを記載しますので、解決方法をご教授頂けたらと思います。
以上、宜しくお願い致します。

new.html.haml

.items = form_with model: @item,local: true do |f| .items__box .items__box--list %p.name 出品画像 %p.need 必須 .items__box--image = f.fields_for :item_images,@item.item_images.build do |i| %label{class: "input-box__image"} %i.fa.fa-camera = item_image.file_field :image %input{type: "file"} %input{type: "file"} %p クリックしてファイルをダウンロード %p 画像がありません

マイグレーションファイル

class CreateItemImages < ActiveRecord::Migration[6.0] def change create_table :item_images do |t| t.string :image t.references :item,null:false,foreign_key:true t.timestamps end end end

items_controller.rb

class ItemsController < ApplicationController def index @items = Item.all end def new @item = Item.new @item.item_images.build end def create @item = Item.create(item_params) @item.save redirect_to root_path end private def item_params params.require(:item).permit(:name, :description,:brand,:condition,:shipping_area,:shipping_method,:shipping_data,:price,:user,:seller,:buyer,item_image_attributes: [:image]) end end

item_image.rb

class ItemImage < ApplicationRecord belongs_to :item end

item.rb

class Item < ApplicationRecord extend ActiveHash::Associations::ActiveRecordExtensions has_many :item_images accepts_nested_attributes_for :item_images end

送信時のターミナル

Parameters: {"authenticity_token"=>"M1ivPeCElTFGwdUCzs33xCWY+lfMHga412dtZL1H1acyfA2giylXaQR39Q10Mjj7A8uJn1luEY6jfa5dQ9W+4A==", "item"=>{"item_images_attributes"=>{"0"=>{"image"=>#<ActionDispatch::Http::UploadedFile:0x00007f8cc35e27f0 @tempfile=#<Tempfile:/var/folders/9z/x6gm4xc952bf07dpsjws794h0000gn/T/RackMultipart20200903-38634-g0fgfi.jpg>, @original_filename="猫.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"item[item_images_attributes][0][image]\"; filename=\"\xE7\x8C\xAB.jpg\"\r\nContent-Type: image/jpeg\r\n">}}, "name"=>"仮", "description"=>"test", "shipping_method"=>"1", "brand"=>"aa", "condition"=>"1", "shipping_area"=>"2", "shipping_data"=>"2", "price"=>"11"}, "commit"=>"出品する"}

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

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

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

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

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

guest

回答1

0

ベストアンサー

,item_image_attributes: [:image] でなく
,item_images_attributes: [:image] ではないかと

has_many :item_images ですから

投稿2020/09/03 14:26

編集2020/09/03 14:27
winterboum

総合スコア23329

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

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

akito_tl

2020/09/03 22:15

上記に変更しましたら、無事解決致しました。 学習進める事ができます。ありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問