Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8262939
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T03:55:37+00:00 2026-06-08T03:55:37+00:00

I have nested resources with 2 models – products and product_images. I’m using Carrierwave

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T03:55:38+00:00Added an answer on June 8, 2026 at 3:55 am

    Your image_tag line is wrong. It should be

    image_tag product.product_images.first.image_url
    

    It’s right there in the examples in Capybara’s README 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a nested resource that looks like this: resources :events resources :attendances post
I have a model called newbie. The routes.rb file looks like: Myapp::Application.routes.draw do resources
Is it possible for a model to belong_to, two models and have a nested
I'm using the friendly_id gem. I also have my routes nested: # config/routes.rb map.resources
I have a Problem with Nested Resources. 2 Models User => has_many :stuffs Stuff
If you have a nested resource defined like this: map.resources :magazines, :has_many => :ads.
I have nested resources like this in my routes.rb - ( my rake:routes gist
I have some nested models that look something like: class Company has_many :managers end
I have the following rails 3 nested models: resources :books do resources :authors end
I am attempting to get rails routes paths via nested resources. I have looked

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.