I have nested resources with 2 models – products and product_images. I’m using Carrierwave to upload the images.
My products model looks like this:
class Product < ActiveRecord::Base
has_many :product_images, :dependent => :destroy
accepts_nested_attributes_for :product_images, :allow_destroy => :true
end
My product_images model looks like this:
class ProductImage < ActiveRecord::Base
mount_uploader :image, ImageUploader
belongs_to :product
end
My products_controller looks like this:
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
...
This is all fine working in my Show view, also with the thumb versions.
However I can’t manage to get the index view working. I’m trying to display the first image in the Product_images table but it won’t work out.
Here’s my index view:
<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>
My routes first looked like this:
resources :products
then I tried this:
resources :products do
resources :product_images
end
How can I make the images be accessible in my index view? Could anybody help me please to fix this?
Thanks a lot!
Your
image_tagline is wrong. It should beIt’s right there in the examples in Capybara’s README 🙂