0

我有 2 个模型的嵌套资源 - 产品和 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
...

在我的显示视图中,这一切都很好,拇指版本也是如此。但是我无法让索引视图正常工作。我正在尝试在 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 

然后我尝试了这个:资源:产品做资源:产品图像结束

如何使图像可以在我的索引视图中访问?有人可以帮我解决这个问题吗?

非常感谢!

4

1 回答 1

0

你的image_tag线路错了。它应该是

image_tag product.product_images.first.image_url

它就在 Capybara 的自述文件中的示例中:)

于 2012-07-19T09:34:32.180 回答