I am using paperclip for image upload:
I am getting the error:
Started GET "/assets/audios/thumbnails/7/thumb/4_X_4.jpg?1345530644"
for 127.0.0.1 at 2012-08-21 12:03:04 +0530
Served asset /audios/thumbnails/7/thumb/4_X_4.jpg - 404 Not Found (1ms)<br/>
ActionController::RoutingError (No route matches [GET] "/assets/audios/thumbnails/7/thumb/4_X_4.jpg"):<br/>
In my model as:
has_attached_file :thumbnail,
:styles => {:medium => "300x300>", :thumb => "100x100>"},
:url => "assets/:class/:attachment/:id/:style/:basename.:extension",
:path => ":rails_root/assets/:class/:attachment/:id/:style/:basename.:extension"
This
:url => "assets/...",
:path => ":rails_root/assets/..."
should be
:url => "/assets/...",
:path => ":rails_root/public/assets/..."
Although it's a terrible idea to save attachments in the assets dir. You can have another, like files inside public:
:url => "/files/:class/:attachment/:id/:style/:basename.:extension",
:path => ":rails_root/public/files/:class/:attachment/:id/:style/:basename.:extension"
Saving inside public/files and using /files url will make the webserver hit the files before rails (of course, correctly configuring your web server, e.g. nginx, is another issue)
I found the correct solution after searching on web continuously. I tried to handle this problem by all probabilities which I could think of like:- permissions, correcting paths,changing server from webrick to thin. Then I studied about the environments properties.
So, here is the correct answer inside your Production environment set:
config.serve_static_assets = true
This solved the problem.
Following link may help you:Upload image using paperclip
Related
Update:
So this is definitely related to the :hash_data option, specifically the :updated_at segment. Somehow the files are being saved to the S3 bucket with a different :updated_at value than Paperclip uses to read the file. Could this be due to some race condition, considering that it occurs intermittently? As I mentioned below, this issue began after upgrading Paperclip to 4.2.1.
I will greatly appreciate any thoughts/tips you guys have on this. Thank you!
When uploading images via Paperclip to S3 bucket, it sometimes saves the files with a different filename than that returned by the attachment#url method. For example, an image is saved to
main_event_photos_46_47fd4f3c2fea17fbb7a0bd27c648911557f9e12b_main.png
However calling #event.main_event_photo.url(:main) returns
main_event_photos_46_15744de74a36207b672356b5ad4c6b30eb4ba85f_main.png
So as you can see, the :hash section of the interpolation does not match, and I have no way of finding the actual url besides opening the bucket in the S3 console. This issue seems to occur about half the time. Sometimes uploading the exact same file does save properly, and the url method accesses it correctly.
This issue began occurring after we upgraded Rails/Ruby/Paperclip. We're now using:
Ruby 2.1.5
Rails 4.2.0
Paperclip 4.2.1
Note that on development, files always save correctly (local filesystem). I have scoured Stackoverflow and Google to no avail. Please let me know if I can provide any additional information. Thank you!
EDIT:
Model:
has_attached_file :main_event_photo, {
:styles => { :original => {:geometry => "1280x800#", :format => 'png'},
:main => {:geometry => "640x400#", :format => 'png'},
:thumb => {:geometry => "330x220#", :format => 'png'}
},
:convert_options => {:original => '-quality 80',
:main => '-quality 80',
:thumb => '-quality 80'
},
:default_style => :main
}.merge!(PAPERCLIP_STORAGE_OPTIONS) # this is defined in the config/environments
validates_attachment_content_type :main_event_photo, :content_type => ['image/jpeg', 'image/png', 'image/gif', 'image/x-png', 'image/pjpeg']
validates_attachment_presence :main_event_photo
Form (basically):
<%= simple_form_for(#event, :url => { :action => #event.id.nil? ? "create" : "update" }) do |f| %>
<%= f.file_field :main_event_photo %>
<% end %>
Note we have many models with Paperclip attachments, and the issue occurs on each.
So this turned out to be the result of a bug. I upgraded Rails to 4.2.1.rc1 which was released last week, and the issue was resolved. If anyone wants more information, check out the thread on Github: https://github.com/thoughtbot/paperclip/issues/1772. It includes a workaround for those who can't upgrade Rails.
I'm tying to get a cloudfront distribution wired up with a custom domain to paperclip but it's not generating my url properly. Here is my configuration:
# initializers/paperclip_defaults.rb
Paperclip::Attachment.default_options.merge!({
:storage => :s3,
:s3_credentials => "#{Rails.root}/config/amazon_s3.yml",
:bucket => "my-image-bucket",
:path => "production/attachments/:attachment/:id/:style/:basename.:extension",
:url => ":s3_host_alias",
:s3_headers => {'Cache-Control' => 'max-age=2147483648'},
:s3_protocol => :https,
:s3_host_alias => "cdn.mydomain.com",
})
According to the docs and every blog post I've read this should be correct. The problem is that it winds up generating this:
https://s3.amazonaws.com/my-image-bucket/production/products/images/44/original/my-image.jpg
The only way I've gotten it to use the CNAME is by specifying s3_host_name which according to the documentation is only used for the Tokyo region. And this also breaks uploads. Any ideas as to what I'm doing wrong?
I'm using Paperclip 3.5.2.
UPDATE
I'm debugging inside attachment.rb#url and here's what I'm seeing. Paperclip::Attachment.default_options[:url] returns cdn.mydomain.com which is correct. However, #options[:url] is returning :s3_path_url instead of s3_host_alias. For some reason that default option is not being merged in properly. However the s3_host_alias option is since #options[:s3_host_alias] is returning cdn.mydomain.com as expected.
So it turns out that :s3_alias_url is the correct token to use for the url and not :s3_host_alias. Not sure where that's documented though. Had to look at s3.rb to figure it out.
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.
I'm using the paperclip gem (3.0.4) which suddenly stopped working as it is no longer creating the thumb folder, only original. Because of this my code can't find the image that was uploaded and errors out.
In my model I have:
has_attached_file :image,
:whiny => false,
:path => ":rails_root/public/system/:attachment/:id/:style/:filename",
:url => "/system/:attachment/:id/:style/:filename",
:styles => { :thumb => "150x100#" }
This code hasn't changed, and when I attempt to render my view I get the following error:
/public/system/images/52/thumb/2011-03-16_07-49-52_412.jpg not found
Looking in my file system I have, /public/system/images/52/original/2011-03-16_07-49-52_412.jpg
Could this be an issue with ImageMagick?
Any idea what is causing this and how to resolve it to ensure the thumb folder is being created? Thanks for your time and assistance.
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')