Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Paperclip uploads 404ing

I'm not sure if I understand how Paperclip works, but as far as I can tell, the default place to save uploads is in the /public folder. When I upload my photo on my development, they appear correctly in (and I can open them in the file structure):

/public/bookmarks/:id/:style.:extension

However, when I do bookmark.photo.url, I get something like:

/system/bookmarks/thumbs/000/000/042/original/filename.png?1362768439

Here's my bookmark.rb:

has_attached_file   :photo,
                        :styles => { :medium => ["512x512>", :jpg], :thumb => ["200x200#", :jpg] }
                        :default_url => "public/bookmarks/default/:style.png",
                        :path => "assets/content/bookmarks/:id/:style.:extension"

Am I missing something here? Isn't Paperclip meant to deal with this stuff for me, or have I got something wrong in the config?

UPDATE

If I add the :path and :url in the first answer, I get:

<img src="/assets/bookmarks/44/original.jpg?1362775508">

Whereas I should be getting:

<img src="/bookmarks/44/original.jpg?1362775508">

But, if I comment out the :url option, instead of getting:

This, which is the default size

this, which is the default not-found image size, I get:

enter image description here

this, which is the same width as the image I have on my filesystem, but the wrong height, and not found. This happens in both Chrome and Safari with clean caches. When I go to the URL, I get a 404. I can also confirm that the image is stored correctly on the filesystem and is viewable from the back end.

like image 407
Alfo Avatar asked Nov 12 '22 09:11

Alfo


1 Answers

The behavior you describe it's a little weird. I suggest you set both :url and :path in a way similar to this:

url: '/:class/:id/:style.:extension',
path: ':rails_root/public:url'

This means images will be stored in:

"#{Rails.root}/public/bookmarks/:id/:style.:extension"

And the URL will give you something like:

/bookmarks/bookmarks/1/thumbs.png

Note that you can do this using config.paperclip_defaults in "application.rb", so you don't have to do it on each model. And you can override this on "production.rb" if you want a different path or storage, e.g.:

  config.paperclip_defaults = config.paperclip_defaults.merge({
    storage: :s3,
    path: 'project_name/public:url'
  })
like image 94
Leonel Galán Avatar answered Dec 03 '22 22:12

Leonel Galán