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

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

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

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

Q&A

解決済

1回答

1067閲覧

Railsでrefileの画像表示について

teetee

総合スコア7

Ruby on Rails

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

0グッド

1クリップ

投稿2019/08/01 12:31

前提・実現したいこと

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/ツールのバージョンなど)

ここにより詳細な情報を記載してください。

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

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

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

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

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

guest

回答1

0

ベストアンサー

product.jacketsはhas_manyなJacketの集合でJacket単体ではありません。
imageを持っているのはJacketインスタンスなのでimage表示が出来ません。

個別のJacketインスタンスに対してattachment_image_tagを呼びましょう

<% @products.each do |product| %> <% product.jackets.each do |j| %> <%= attachment_image_tag j, :image, :fill, 60, 60 %> <% end %> <% end %>

投稿2019/08/02 01:57

hellomartha

総合スコア329

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問