Jump to content

The ultimate community for Ruby on Rails developers.


Photo

rendering a collection of images from a collection of items.


  • Please log in to reply
No replies to this topic

#1 Esteban

Esteban

    Passenger

  • Members
  • 1 posts

Posted 13 February 2014 - 03:26 PM

Hello guys, i'm new on RoR and i'm making my first app that renders a list of items with images.

 

I have a table list that display items with basic information (name, description, etc.) and y have a Detail page that shows single information of each item, images included (i'm using the paperclip gem). By model, each item is associated with multiple images, rendering this images on de Details page:

 

Details Page

<%= form_for @item , :html => { :multipart => true } do |f| %>

 

<%= image_tag @item.images.first.image.url %>

<%= image_tag @item.images.second.image.url %>

<%= image_tag @item.images.third.image.url %>

<%= @item.name %>

<%= @item.description %>

 

<% end %>

 

 

Now, i want to render just the first image of each item in my table list. I'm listing my information like this

 

List Page
<% @items.each do |item| %>
<tr>
  <td><%= item.name %></td>
  <td><%= item.description %></td>
</tr>
<% end %>
 
So I tried this
 
<% @items.each do |item| %>
<tr>
  <td><%= item.name %></td>
  <td><%= item.description %></td>
  <td><%= item.images.first.image.url %></td>
</tr>
<% end %>
 
But I got this error
 
undefined method `cover' for nil:NilClass
 
I don't know why is not working in the list but in the Details page.
 
my controller is simple
 
def index
  @items = Item.find(:all)
end
 
Thanks in advance!





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users