前提・実現したいこと
現在フリマアプリの制作をしています。
1つの投稿に対して画像が複数になるのでテーブルを分けて進めています。
他テーブルに保存しているimage をindexで表示したいのですが、ノーメソッドエラーになり表示ができません。
発生している問題・エラーメッセージ
undefined method `item_imgs' for nil:NilClass
該当のソースコード
model
1class Item < ApplicationRecord 2 has_many :item_imgs, dependent: :destroy 3end
model
1class ItemImg < ApplicationRecord 2 belongs_to :item 3end
controller
1class ItemsController < ApplicationController 2 def index 3 @items = Item.limit(3).order(created_at: "DESC") 4 end 5end
view
1.new__item__list 2 - @item.item_imgs.each do |item_img| 3 = link_to root_path do 4 =image_tag item_img.url,class: "new__item__image"
itemsテーブル
id | name | price |
---|---|---|
1 | パーカー | 500 |
item_imgsテーブル
id | url | item_id |
---|---|---|
1 | hogehoge | 1 |
試したこと
コントローラーの定義に
@item_img = @item.item_imgを追加
@item_img =ItemImg.allを追加
他テーブルの値の表示を調べても、showアクションの内容が多く、今自分がやろうとしていることについてどう解決すれば良いのか分からず質問しました。質問内容わかりづらいかもしれませんが回答よろしくお願いいたします。
![guest](/img/icon/icnUserSample.jpg)
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。