前提・実現したいこと
ecサイトを作成しています。
gem refileを使って、画像を表示させたいのですが、エラーが出ます。
gem cocoonを使い、Productという親モデルにJacketという子モデルをネストさせています。
createはでき、データは保存されている状態です。
発生している問題・エラーメッセージ
undefined method `image' for #<ActiveRecord::Associations::CollectionProxy []>
該当のソースコード
products/index.html.erbです。
<div class="container"> <% @products.each do |product| %> <%= attachment_image_tag product.jackets, :image, :fill, 60, 60 %> <% end %> </div>
products_controllerです。
class ProductsController < ApplicationController def index @products = Product.all.includes(:jackets) end def new @product = Product.new @disk = @product.disks.build @sing = @disk.sings.build @artist = @product.artists.build @label = @product.labels.build @genre = @product.genres.build @jacket = @product.jackets.build end def create @product = Product.new(product_params) if @product.save redirect_to home_top_path else render "new" end end def edit @product = Product.find(params[:id]) end def update @product = Product.find(params[:id]) if @product.update(product_params) redirect_to home_top_path else render "edit" end end private def product_params params.require(:product).permit(:format, :title, :stock, :status, :price, disks_attributes: [:id, :disk_name, :product_id, :_destroy, sings_attributes: [:id, :disk_id, :sing_name, :sing_order, :_destroy]], artists_attributes: [:id, :product_id, :artist_name, :_destroy], labels_attributes: [:id, :product_id, :label_name, :_destroy], genres_attributes: [:id, :product_id, :genre_name, :_destroy], jackets_attributes: [:id, :image, :product_id, :_destroy]) end end
models/Product.rbです。
class Product < ApplicationRecord has_many :carts has_many :order_histories has_many :disks has_many :genres has_many :jackets has_many :artists has_many :labels accepts_nested_attributes_for :disks, reject_if: :all_blank, allow_destroy: true accepts_nested_attributes_for :artists, reject_if: :all_blank, allow_destroy: true accepts_nested_attributes_for :genres, reject_if: :all_blank, allow_destroy: true accepts_nested_attributes_for :labels, reject_if: :all_blank, allow_destroy: true accepts_nested_attributes_for :jackets, reject_if: :all_blank, allow_destroy: true accepts_attachments_for :jackets, attachment: :image #refileで必要な記述 validates :format, presence: true validates :title, presence: true validates :stock, presence: true validates :status, presence: true validates :price, presence: true end
models/jacket.rbです。
class Jacket < ApplicationRecord belongs_to :product attachment :image end
db/schema.rbです。
create_table "products", force: :cascade do |t| t.string "format" t.string "title" t.integer "stock" t.string "status" t.integer "price" t.datetime "created_at", null: false t.datetime "updated_at", null: false end create_table "jackets", force: :cascade do |t| t.integer "product_id" t.datetime "created_at", null: false t.datetime "updated_at", null: false t.text "image_id" end
試したこと
accepts_attachments_for という記述が抜けていたので、足したのですが変わりませんでした。
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。