nadia
nadia

Reputation: 2539

How to display paperclip's uploads.

Im currently building a blog. While the paperclip photos pop up inside the individual blog posts they don't seem to pop up in the index, where all the blog posts are displayed. 13th line seems to be the problem.

this is my index.html.erb for my posts

10   <% @posts.each do |post| %>
11       <tr>
12       <td><%= link_to  post.title, post_path(post) %></td>
13       <td><%= image_tag @post.picture(:medium) %></td>
14       <td><%= post.text %></td>
15       <td><%= link_to 'Show', action: :show, id: post.id %></td>
16       <td><%= link_to 'Edit', action: :edit, id: post.id %></td>
17       <td><%= link_to 'Destroy', { action: :destroy, id: post.id },
18                       method: :delete, data: { confirm: 'Are you sure?' } %></td>

it throws me a no method error.

Please let me know if I can give you extra files if you need more information Thank you in advance!

Upvotes: 1

Views: 75

Answers (3)

Jon
Jon

Reputation: 10898

You're not passing the URL for the image to the image_tag helper.

You need to display your image like this:

<%= image_tag post.picture.url(:medium) %>

Upvotes: 2

Richard Peck
Richard Peck

Reputation: 76774

As mentioned by Jon, you need to use the .url method on your picture object (official documentation):

<%= image_tag post.picture.url(:medium) %>

Paperclip

The reason for this is Paperclip actually creates its own picture object & attaches several methods to that (url being one of them). This means you have to call url each time you show the post's image to get it to load

Normally, you'd be able to call methods on your instance or local variables, but as Paperclip actually creates its own object, you have to use its in-built methods to get it to work correctly

Upvotes: 1

zeantsoi
zeantsoi

Reputation: 26193

You're trying to access the @post instance variable, but that instance variable doesn't exist. Rather, within your loop, you should access your post local variable as such:

<%= image_tag post.picture.url(:medium) %>

Note also that, from the documentation, the correct syntax to access variations of a Picture instance involves passing the variation name to the url attribute:

post.picture(:medium) # Invalid
post.picture.url(:medium) # Valid!

Upvotes: 2

Related Questions