2012-07-19 192 views
0

我有嵌套資源與2款與索引視圖的問題 - 產品和product_images。我正在使用Carrierwave上傳圖像。 我的產品型號如下:的Rails:在嵌套資源

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 

然後我嘗試這樣: 資源:產品做 資源:product_images 結束

我怎樣才能使圖像在我的索引視圖中可訪問?有人可以幫我解決這個問題嗎?

非常感謝!

回答

0

image_tag線是錯誤的。它應該是

image_tag product.product_images.first.image_url 

這是正確的,在the examples in Capybara's README :)

+0

有時可以如此簡單!我嘗試了兩天的一切,我的路線模型和控制器。學習Rails還有很長的路要走! 非常感謝您的幫助! – 2012-07-19 09:49:18

+0

不用擔心。您可以將問題標記爲已回答,順便說一句:) – 2012-07-19 10:04:24