I use paperclip in my app, but my controller tests are failing because of:
BlogsControllerTest#test_should_update_blog:
Paperclip::AdapterRegistry::NoHandlerError: No handler found for "/images/original/missing.png"
/Users/user/.rvm/gems/ruby-2.1.2/gems/paperclip-3.5.2/lib/paperclip/io_adapters/registry.rb:19:in `handler_for'
/Users/user/.rvm/gems/ruby-2.1.2/gems/paperclip-3.5.2/lib/paperclip/io_adapters/registry.rb:29:in `for'
/Users/user/.rvm/gems/ruby-2.1.2/gems/paperclip-3.5.2/lib/paperclip/attachment.rb:96:in `assign'
I'm not sure where I should place the missing.png
image in my code, I tried in public/assets/original/missing.png
but it doesn't seem to manage it.
Also there's something odd: I have a paperclip.rb
initializer line:
Paperclip::Attachment.default_options[:default_url] = "/images/default_image.png"
but still the app is looking for the missing.png
UPDATE: ok I figured that the default_url was overridden in the model:
has_attached_file :image, styles: { big: "1200X630>", thumb: "150X150" }, default_url: "/images/:style/missing.png"
I still don't know where to place the image.
UPDATE2:
the entire paperclip initializer:
Paperclip::Attachment.default_options[:styles] = { thumb: "100x100#", small: "200x200#", screen: "800x600#"}
Paperclip::Attachment.default_options[:default_url] = "/images/missing.png"
Paperclip::Attachment.default_options[:path] = ":rails_root/public/assets/:class/:attachment/:id_partition/:style/:hash.:extension"
Paperclip::Attachment.default_options[:url] = "/assets/:class/:attachment/:id_partition/:style/:hash.:extension"
Paperclip::Attachment.default_options[:hash_secret] = "XXXXXXXXXXXXXXXXX"
Paperclip.registered_attachments_styles_path = "public/assets/paperclip_attachments2.yml"
UPDATE3: checking the paperclip code that actually rises the code, the exception is risen by this piece of code, which is basically testing all the adapters available, the one that looks like the closest to what I want to do is the fileAdapter
which tests if the string passed is a File.
I'm quite surprised from finding this, I thing I might be getting something wrong here. If I exchange the initializer line to:
Paperclip::Attachment.default_options[:default_url] = File.new "public/images/missing.png"
then the exception is different:
BlogsControllerTest#test_should_update_blog:
NoMethodError: undefined method `gsub' for #<File:public/images/missing.png>
/Users/user/.rvm/gems/ruby-2.1.2/gems/paperclip-3.5.2/lib/paperclip/interpolations.rb:33:in `block in interpolate'
/Users/user/.rvm/gems/ruby-2.1.2/gems/paperclip-3.5.2/lib/paperclip/interpolations.rb:32:in `each'
UPDATE4: this is what the test looks like:
test "should update blog" do
put :update, id: @blog, blog: {
author_id: @blog.author_id,
body: @blog.body,
image: @blog.image,
title: @blog.title
}
assert_redirected_to blog_path(assigns(:blog))
end
test "should create blog" do
assert_difference('Blog.count') do
post :create, blog: {
author_id: @blog.author_id,
body: @blog.body,
image: @blog.image,
title: @blog.title }
end
assert_redirected_to blog_path(assigns(:blog))
end
then:
@blog.image.class
=> Paperclip::Attachment
@blog.image.url
=> "/images/missing.png"
For this line of code:
Paperclip::Attachment.default_options[:default_url] = "/images/default_image.png"
Paperclip is looking for images under /public/images/default_image.png
so you have defined style big
and thumb
. Paperclip will look for img in public/images/thumb/default_image.png
or public/images/big/default_image.png
depending what style will you call in <% image_tag @model.image.url(:style) %>
.
gotcha! If you want model to use :default_url
don't send :image
in params. remove image from params and lets see how it goes`
In Rails 5, I managed to work using :style in the path:
Paperclip::Attachment.default_options[:default_url] = "/images/folder/:style/missing.png"
and if, for example, this image doesn't exist:
<%= image_tag @photo.picture.url(:medium) %>
the result is
/images/folder/medium/missing.png
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With