0

私は2つのモデル(productsとproduct_images)でリソースをネストしました。Carrierwaveを使用して画像をアップロードしています。私の製品モデルは次のようになります。

class Product < ActiveRecord::Base

has_many :product_images, :dependent => :destroy
accepts_nested_attributes_for :product_images, :allow_destroy => :true
end

私のproduct_imagesモデルは次のようになります。

class ProductImage < ActiveRecord::Base
mount_uploader :image, ImageUploader
belongs_to :product
end

私のproducts_controllerは次のようになります。

class ProductsController < ApplicationController

def index
@products = Product.all
end

def show
@product = Product.find(params[:id])
@images = @product.product_images
end

def new
@product = Product.new
@product.product_images.build
end
...

これは、私のShowビューでも、thumbバージョンでも問題なく機能します。しかし、インデックスビューを機能させることができません。Product_imagesテーブルの最初の画像を表示しようとしていますが、うまくいきません。

これが私のインデックスビューです:

<table>

<% @products.each do |product| %>
<tr class="<%= cycle('list_line_odd', 'list_line_even') %>">

<td class="list_description">
<dl>
<dt><%= product.title %></dt>
</dl>
</td>
<td>
<%= image_tag(product.product_image.first ) %>
</td>
</td>
...
</table>

私のルートは最初は次のようになりました。

resources :products 

それから私はこれを試しました:resources:products do resources:product_images end

インデックスビューで画像にアクセスできるようにするにはどうすればよいですか?誰かがこれを修正するのを手伝ってくれませんか?

どうもありがとう!

4

1 に答える 1

0

あなたのimage_tag行は間違っています。そのはず

image_tag product.product_images.first.image_url

Capybara の README の例の中にあります:)

于 2012-07-19T09:34:32.180 に答える