的Rails:在嵌套资源
问题描述:
我有嵌套资源与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 结束
我怎样才能使图像在我的索引视图中可访问?有人可以帮我解决这个问题吗?
非常感谢!
答
你image_tag
线是错误的。它应该是
image_tag product.product_images.first.image_url
这是正确的,在the examples in Capybara's README :)
有时可以如此简单!我尝试了两天的一切,我的路线模型和控制器。学习Rails还有很长的路要走! 非常感谢您的帮助! – 2012-07-19 09:49:18
不用担心。您可以将问题标记为已回答,顺便说一句:) – 2012-07-19 10:04:24