paperclip where to place the missing.png default image?
Asked Answered
J

2

5

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"
Jacquez answered 11/8, 2014 at 17:41 Comment(14)
Where is 'has_attached_file :image' code?Explicative
Any updates @don guilio ?Bashkir
the has_attached_file code is inside the blogs modelJacquez
Put the image in /public/images/* and make your has_attached_file: default_url: "/image/*" This is wierd though, are you saving a default attachment if none is assigned?Explicative
What do you mean DDDDD? :default_url is commonly used ;)Bashkir
I don't undertand, should I place a * in the default_url string? how will it know which image from that path to pick?Jacquez
@dongiulio if it isn't a private thing please link your repo. I'll try to fix your issue on my own and give you feedback ;)Bashkir
I'm afraid it is a private repo. thanks for the offer though, I've figured something odd in the paperclip code, I'm writing an updateJacquez
No, where I put * you put the file name.Explicative
@Explicative thanks, already tried that.Jacquez
HOw does your test look loke @dongiulioBashkir
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`\Bashkir
Fantastic, that solved it :) thanks a lot, please update your answerJacquez
I have exactly the same initializer line in paperclip.rb, but it says in the model: has_attached_file :attachment and no default_url overridden. Why is the image displayed as a link to missing.png and not as the png itself?Publicize
B
12

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

update:

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) %>.

update #2 (according to update #4 from author's question)

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`

Bashkir answered 11/8, 2014 at 17:47 Comment(8)
ok, I've removed the default image overriding and changed the initializer to "/images/missing.png" and placed an image under /public/images/missing.png, but I'm still getting the error No handler found for "/images/missing.png"Jacquez
Just want to ensure: have u restarted rails server?Bashkir
better sure than sorry :) : I'm seeing the issue when I run the unit tests with rake test the "server" is restarted at every attemptJacquez
Thanks, I've placed the file in these two dirs, and it didn't improve, I updated the question with the entire paperclip initializerJacquez
The error message says it is looking for missing.png inside the public/images/original folder, not just the public/images folder.Arkwright
I just tested it on my own on new rails app and everything was working. I tested with setting up both default_url in initializer or controller. I think you still missed something. are you sure you placed it in ~/dir_to_your_app/public/images/big/missing.png? If you want to define styles like big and small remember to call in <% image_tag @model.image.url(:style) as an argument, it is really important. What is more - what's your error msg right now?Bashkir
so according to what @EgeErsoz pointed it might be possible that you don't specify :style in @model.image.url(:style) method. If you do that than it will look for image in public/images/original. solution is easy: specify :style in .url() method or add missing.png to dir public/images/original/missing.pngBashkir
the view is not involved here, the exception is triggered by the has_attached_file validator in the model, I don't use the default image in my view.Jacquez
D
7

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
Dort answered 26/10, 2016 at 14:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.