I am working on a project that is using Paperclip to upload multiple
images to an Asset model. my Post model has_many Assets. As per this
tutorial/screencast…
This all works fine and I can add/delete multiple images to a post
perfectly. I can also
display multiple images for each post in the show view using this
code…
///////////////////////////
<% for asset in @post.assets %>
<%= link_to image_tag(asset.asset.url(:thumb)),
asset.asset.url(:original) %>
<% end %>
///////////////////////////
What I want to be able to do is display 1 or more images for each of the
records in index.html but I can’t make that code work in index …
anyone got any any idea how I can do this?
This all works fine and I can add/delete multiple images to a post
asset.asset.url(:original) %>
Cheers for any help!
Let’s say you are looping over your Post objects in the index page, and
you want to show the first image for each post:
<% for post in @posts %>
<%= image_tag(post.assets.first.url(:thumb)) %>
...whatever else you show in the index about each post
<% end %>
The key thing here is that post.assets is a collection of all related
assets.
Thanks for your reply Walter. I have tried what you suggested but I get
the following …
undefined method `url’
I think I had tried the way you suggested previously but with the same
result.
Okay, the confusion is arising because you have a relationship named
asset, and within that, a property named asset. Try this:
post.assets.first.asset.url(:thumb).