我在我的应用程序中使用了paperclip,但我的控制器测试由于以下原因而失败:
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'
我不确定将missing.png
图片放在我的代码中的哪个位置,我尝试了public/assets/original/missing.png
,但它似乎无法管理它。
还有一些奇怪的事情:我有一个paperclip.rb
初始化线:
Paperclip::Attachment.default_options[:default_url] = "/images/default_image.png"
但该应用仍在寻找missing.png
更新:好的我认为在模型中覆盖了default_url:
has_attached_file :image, styles: { big: "1200X630>", thumb: "150X150" }, default_url: "/images/:style/missing.png"
我仍然不知道放置图像的位置。
UPDATE2:
整个回形针初始化程序:
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:检查实际上升代码的回形针代码,异常上升this piece of code,这基本上是测试所有可用的适配器,看起来最接近我想做的那个是fileAdapter
测试传递的字符串是否为File。
我发现这一点后感到非常惊讶,我可能会在这里遇到问题。如果我将初始化线交换为:
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:这就是测试的样子:
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"
答案 0 :(得分:11)
对于这行代码:
Paperclip::Attachment.default_options[:default_url] = "/images/default_image.png"
Paperclip正在寻找/public/images/default_image.png
因此您定义了样式big
和thumb
。 Paperclip会在public/images/thumb/default_image.png
或public/images/big/default_image.png
中查找img,具体取决于您在<% image_tag @model.image.url(:style) %>
中调用的样式。
疑难杂症!如果您希望使用模型:default_url
,请不要在参数中发送:image
。从params删除图像,让我们看看它是怎么回事?
答案 1 :(得分:6)
在Rails 5中,我设法在路径中使用:style 工作:
.html
如果,例如,此图像不存在:
Paperclip::Attachment.default_options[:default_url] = "/images/folder/:style/missing.png"
结果是
<%= image_tag @photo.picture.url(:medium) %>