回形针放置missing.png默认图像的位置? [英] paperclip where to place the missing.png default image?
问题描述
我在我的应用程序中使用了paperclip,但我的控制器测试失败的原因是:
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'
我不确定在哪里放置。 png
我的代码中的图片,我试过 public / assets / original / missing.png
但它似乎没有管理它。
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.
还有一些奇怪的事情:我有一个 paperclip.rb
初始化线:
Also there's something odd: I have a paperclip.rb
initializer line:
Paperclip::Attachment.default_options[:default_url] = "/images/default_image.png"
但应用程序仍然在寻找缺失.png
but still the app is looking for the missing.png
更新:确定我认为在模型中覆盖了default_url:
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:检查实际上升代码的回形针代码,异常上升为这段代码,它基本上测试了所有可用的适配器,看起来最接近我想做的那个是 fileAdapter
测试传递的字符串是否为File。
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"
然后异常不同:
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:这是测试的样子:
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
然后:
@blog.image.class
=> Paperclip::Attachment
@blog.image.url
=> "/images/missing.png"
推荐答案
这条线代码:
Paperclip::Attachment.default_options[:default_url] = "/images/default_image.png"
Paperclip正在寻找 /public/images/default_image.png $ c $下的图片c>
Paperclip is looking for images under /public/images/default_image.png
因此您定义了样式 big
和 thumb
。 Paperclip将在 public / images / thumb / default_image.png
或 public / images / big / default_image.png
取决于您在<%image_tag @ model.image.url(:style)%>
中调用的样式。
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) %>
.
陷阱!如果您希望模型使用:default_url
请勿在params中发送:image
。从params删除图像,让我们看看它是怎么回事?
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`
这篇关于回形针放置missing.png默认图像的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!