PaperClip Issues – Ruby on Rails

I finally decided to replace all my existing code to handle images with Paperclip. I was following the screencast by Emerson Lackey, #134 Paperclip, however I ran into a couple of issues. Thankfully they were all very easily fixed, and probably wouldn’t have occurred at all if I’d simply watched the whole screencast instead of trying to rush on ahead.

Issue #1:
The first issue I encountered was that my asset fields weren’t appearing on the page:

1
2
3
4
5
6
7
<% f.fields_for :assets do |asset_fields|   %>
      <% if asset_fields.object.new_record? %>
        <p>
          <%= asset_fields.file_field :asset %>
        </p>
      <% end %>
    <% end %>

I spent way too much time trying to work this one out, especially considering how obvious the solution is – the screencast is simply missing an “=” after the initial “<%" in "<% f.fields_for". Simply amend it as follows:

1
2
3
4
5
6
7
<%= f.fields_for :assets do |asset_fields|   %>
      <% if asset_fields.object.new_record? %>
        <p>
          <%= asset_fields.file_field :asset %>
        </p>
      <% end %>
    <% end %>

Issue #2:
The second one is actually encountered and addressed by Emerson himself. Unfortunately I started trying to find a solution before seeing his, so just in case someone else does a Google search I’ll include the issue here:

undefined method `symbolize_keys!' for "/system/assets/1/original/mack_truck.jpg?1329550205":String

     <% f.fields_for :assets do |asset_fields| %>
       <% unless asset_fields.object.new_record? %>
       

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

<% end %> <% end %>

The problem here is simply that the parenthesis are in the wrong place, simply amend your code as follows:

1
2
3
4
5
6
7
     <% f.fields_for :assets do |asset_fields| %>
       <% unless asset_fields.object.new_record? %>
       <p>
         <%= link_to image_tag(asset_fields.object.asset.url(:thumb)), asset_fields.object.asset.url(:original) %>
       </p>
       <% end %>
     <% end %>

Anyway, hopefully this helps someone else out – let me know if there are any issues.

Update: Don’t name your model assets!
Unfortunately I followed the tutorials naming example and called my model Asset. While this may have been fine in earlier version of rails it causes quirky conflicts with the asset pipeline. I strongly encourage you to use a different name i.e. Uploads

One thought on “PaperClip Issues – Ruby on Rails

  1. Daryl

    I was having the problem that the file upload fields were not showing up.

    You saved me a lot of time. Thank you.

    Reply

Leave a Reply