1
votes

I'm using Paperclip with my Rails 3.0 app in order to add an avatar to a user, and it won't save the image because the paths are off. Heres what I get:

Started GET "/profilepics/small/missing.png" for 127.0.0.1 at 2012-04-11 23:38:29 -0700

ActionController::RoutingError (No route matches "/profilepics/small/missing.png"):

My user model has:

    has_attached_file :profilepic, :styles => { :small => "150x150>" }

What should I put for :path => & :url => ?

Form looks like :

      <% form_for @user, :html => { :multipart => true } do |f| %>

      <%= f.file_field :profilepic %>

      <% end %>

Logs look like :

Started GET "/system/profilepics/small/missing.png" for 127.0.0.1 at 2012-04-12 00:33:51 -0700

ActionController::RoutingError (No route matches "/system/profilepics/small/missing.png"):

Rendered /usr/lib/ruby/gems/1.9.1/gems/actionpack-3.0.12/lib/action_dispatch/middleware/templates/rescues/routing_error.erb within rescues/layout (1.2ms)

3
You don't necessarily need to specify :path or :url, by default the images are saved to :rails_root/public/system/:attachment/:id/:style/:filenameDanS
I just keep getting a broken img upon uploading...:(Elias7
Could you add your view code as well please?DanS
Sure, view is <%= image_tag @user.profilepic.url(:small) %>Elias7

3 Answers

3
votes

See my sample:

   has_attached_file :avatar, :styles => { :thumb => "50x50#", :large => "1000x1000>", :medium => "200x200#" },
     :default_url => "/system/avatars/:style/missing.png",
     :url  => "/system/:attachment/:id/:style_:filename",
     :path => ":rails_root/public/system/:attachment/:id/:style_:filename"
  • :default_url - is the path for the default image, when the user didn't upload any avatar
  • "#" - this symbol is to crop image

then you can show your images as such:

<%=image_tag(@user.avatar.url(:thumb))%>
<%=image_tag(@user.avatar.url(:medium))%>
1
votes

Works now!!!

For people struggling with the same issue, here's a few important things to ALWAYS make sure and check:

  1. In your form, always specify { :multipart => true } otherwise, the form won't accept file attachments.

    <%= form_for @user, :html => **{ :multipart => true }** do |f| %>
    
  2. In your user.rb (or whatever model you want to add attachments to ), make attr_accessible :photo (or whatever you name your attachment)

  3. ALWAYS restart your server after installing a new Gem.

:) Thanks guys!!!!

0
votes

There is no need to given url and path option if you simply want to display an image.

Use this line in show page and it will display image...

     <%=image_tag(@user.profilepic.url(:small))%>

And enjoy..............