Multiple Paperclip Images on Index

Hi,

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...

https://github.com/Emerson/Multiple-File-Uploads-with-Paperclip-and-Rails-3/blob/master/app/views/posts/show.html.erb">

http://www.emersonlackey.com/article/paperclip-with-rails-3>

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 %>

    <%= link_to image_tag(asset.asset.url(:thumb)),
asset.asset.url(:original) %>

  <% end %>
</div>

///////////</code>////////////////

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!

Hi,

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...

https://github.com/Emerson/Multiple-File-Uploads-with-Paperclip-and-Rails-3/blob/master/app/views/posts/show.html.erb">

http://www.emersonlackey.com/article/paperclip-with-rails-3>

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 %>

   <%= link_to image_tag(asset.asset.url(:thumb)),
asset.asset.url(:original) %>

<% end %>
</div>

///////////</code>////////////////

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.

Walter

<% 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).

Walter

Thank you very much for your help Walter!

That has solved it and I have hopefully learnt something very useful :slight_smile: