Paperclip giving wrong url. How to fix? - ruby-on-rails

My paperclip path in the model is:
has_attached_file :image, :path => ":rails_root/app/assets/images/article_images/:id/:style_:basename.:extension"
But when I call article.image.url, here is the url I get (which is broken):
/system/images/64294/original/fantastik.jpg?1324288670

You also need to setup the URL, what you want is:
has_attached_file :image,
:path => ":rails_root/app/assets/images/article_images/:id/:style_:basename.:extension"
:url => "/app/assets/images/article_images/:id/:style_:basename.:extension"
The only thing I would question is if you're storing them in a good place. Typically, they would go in
/public/...
Or on another service like s3. However, that is how you modify the URL
Hope this helps!

like #andrewpthorp mention that's because you switch only path, but you can do it more DRY
paperclip defaults:
:url => "/system/:attachment/:id/:style/:filename",
:path => ":rails_root/public:url",
You can see that url can be part of path so your config should look like:
has_attached_file :image,
:url => "/app/assets/images/article_images/:id/:style_:basename.:extension",
:path => ":rails_root:url"
Be carefull, usualy servers (apache, nginx) serves files only from public directory.
More options for has_attached_file you can find here

This is how to fix the issue with :default_url:
:default_url => ActionController::Base.helpers.asset_path('empty-event-cover.png')

Related

What mean the params returned from Paperclip in url

I have defined my paths like this with Paperclip:
:path => ":rails_root/public/system/photos/:style/:id.:extension",
:url => "/system/photos/:style/:id.:extension"
Now im wondering for what the params in the file url stand?
For example: 1405493881 in this url:
/system/photos/medium/82.png?1405493881
Thanks!
the last part of the url is anti caching timestamp...
if you want to disable that you can use
# assuming your model have something similar to the following
has_attached_file :photo,
:styles => { ...yada yada styles... },
:path => ":rails_root/public/system/photos/:style/:id.:extension",
:url => "/system/photos/:style/:id.:extension",
:use_timestamp => false
this will not create the timestamp for the url
check this question
also the source code documentation here

Paperclip change default path and hash image names

I'm new to Rails and using Paperclip, I have it set up on my model already without issue. I just don't like the path it's generating for my images right now (:root_path/system/users/avatars/000/000/001) I really don't even understand it. How can I modify this default path for my images to something more friendly? And how can I hash the image names?
In your model, you can set the default path, styles and url as so:
has_attached_file :avatar,
:styles => { :large => "500x500>", :medium => "300x300>", :thumb => "100x100>" },
:path => ":rails_root/public/images/:id/:style/:filename",
:url => "/images/:id/:style/:filename"
you can setup the hash on the paperclip initializer file (config/initializers/paperclip_defaults.rb)
Quoting from paperclip wiki:
Paperclip::Attachment.default_options.update({
:path => ":class/:attachment/:hash/:style.:extension",
:hash_secret => "SOME_RANDOM_SECRET"
})
The :hash part is generated from :hash_secret and the pattern given by the :hash_data option, which by default is ":class/:attachment/:id/:style/:updated_at".

Getting Image Paperclip::Errors::NotIdentifiedByImageMagickError after updating to Paperclip 3.4.0

I got the following message after upgrading:
Paperclip 3.0 introduces a non-backward compatible change in your
attachment path. This will help to prevent attachment name clashes
when you have multiple attachments with the same name. If you didn't
alter your attachment's path and are using Paperclip's default, you'll
have to add :path and :url to your has_attached_file definition.
For example:
has_attached_file :avatar,
:path => ":rails_root/public/system/:attachment/:id/:style/:filename",
:url => "/system/:attachment/:id/:style/:filename"
So I did so:
post.rb:
has_attached_file :image, :styles => { :medium => "170x300>",
:thumb => "142x185>" },
:path => ":rails_root/public/system/:attachment/:id/:style/:filename",
:url => "/system/:attachment/:id/:style/:filename"
But then I saw this error message:
Image Paperclip::Errors::NotIdentifiedByImageMagickError
I even added this to environments/development.rb:
Paperclip.options[:command_path] = "/usr/bin/"
(which identify outputs /usr/bin/identify)
But still no luck.
What could be the problem?
Wow, I didn't expect this. The problem wasn't due to upgrading.
It was because the file I was uploading was named like this:
Screenshot at 2012-11-26 16:22:44.png
Weird.
The issue is in the filename.
colons are not accepted, if you remove the colon from the attachment name using gsub it'll be accepted always.

How to use rails plus paperclip and fog in localhost

I'm trying to setup my development environment to store and fetch images in local host.
I've manage to save the images on the correct path, but I can't find a way to load the page and retrieve them from the assets pipeline.
I have this on my model:
has_attached_file :cover, :styles => {:small => '80x80'},
:storage => :fog,
:fog_credentials => {:provider => "Local",
:local_root => "#{Rails.root}/public"},
:fog_directory => 'system/migos',
:fog_host => "http://localhost:3000/assets",
:default_url => '/assets/missing/:attachment/missing_:class_:style.png',
:path => ':rails_env/:class/:attachment/:id_partition/:style/:filename'
and the file gets saved correctly to:
public/system/migos/development/workgroups/covers/000/000/011/small/logo.png
When loading the page, it tries to fetch the file from here:
/assets/localhost/development/workgroups/covers/000/000/011/small/logo.png?1346598225
and fails.
What am I missing here?
has_attached_file :photo,
:url => "/assets/vehicles/:id/:style/:basename.:extension",
:path => ":rails_root/public/assets/vehicles/:id/:style/:basename.:extension"
Try the code from above, check the URL and PATH, it will return the next image address:
http://localhost:3000/assets/vehicles/1/original/72854906.jpg?1346092386
the folder structure is assets/vehicles/1/original.

paperclip - modify the path structure for storing images

I would be really grateful if someone could help me with this.
I am using paperclip to upload images.
I modified my paperclip.rb to add the following interpolation code:
Paperclip.interpolates :submission_id do |attachment, style|
attachment.instance.submission_id
end
I have the following code included in the image.rb:
has_attached_file :data, :path => ":rails_root/public/system/datas/:submission_id/:id/:style",
:url => "/system/datas/:submission_id/:id/:style",
:styles => {
:thumb => "50x50#",
:large => "640x480#"
}
Currently, when i upload images, they are stored in the following folder structure:
submission_id/image_primary_id/image -----> 13/244/original
I would like to store the image in the following format:
SUB_submission_id/originals/imagename.jpeg ---> SUB_13/originals/image01.jpeg
Please could someone shed some light on this how to do this.
Thanks a lot for your help
Did you try something like this?
Paperclip.interpolates :submission_id do |attachment, style|
"SUB_#{attachment.instance.submission_id}"
end
And the drop the :id from the path and url (make sure you don't upload files with the same name though)
has_attached_file :data, :path => ":rails_root/public/system/datas/:submission_id/:style",
:url => "/system/datas/:submission_id/:style",

Resources