我在我的应用程序中使用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
更新:好的,我发现默认URL在模型中被覆盖了:
has_attached_file :image, styles: { big: "1200X630>", thumb: "150X150" }, default_url: "/images/:style/missing.png"
我还不知道在哪里放置这张图片。
更新2:
整个附件初始化器:
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: 检查实际上引发异常的paperclip代码,异常是由 这段代码 引起的,它基本上测试了所有可用的适配器,最接近我想要做的是
fileAdapter
,它测试传递的字符串是否为文件。我对此感到非常惊讶,我认为我可能在这里犯了什么错误。如果我将初始化器行替换为:
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'
更新4:这是测试的样子:
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"
:default_url
,不要在参数中发送:image
。请从参数中删除image
,然后看看效果如何。 - Filip Bartuzi