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...
///////////<code>////////////////
<div class="thumb">
<% for asset in @post.assets %>
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?
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...
///////////<code>////////////////
<div class="thumb">
<% for asset in @post.assets %>
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?
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 %>
<tr><td>
<%= image_tag(post.assets.first.url(:thumb)) %>
</td><td>
...whatever else you show in the index about each post
</td></tr>
<% end %>
The key thing here is that post.assets is a collection of all related assets.
<% for post in @posts %>
<tr><td>
<%= image_tag(post.assets.first.url(:thumb)) %>
</td><td>
...whatever else you show in the index about each post
</td></tr>
<% end %>
The key thing here is that post.assets is a collection of all related
assets.
Walter
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.
<% for post in @posts %>
<tr><td>
<%= image_tag(post.assets.first.url(:thumb)) %>
</td><td>
...whatever else you show in the index about each post
</td></tr>
<% end %>
The key thing here is that post.assets is a collection of all related
assets.
Walter
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).