【发布时间】:2014-08-11 17:41:15
【问题描述】:
我在我的应用程序中使用了回形针,但我的控制器测试失败了,原因是:
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"
我仍然不知道在哪里放置图像。
更新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:检查实际上升代码的回形针代码,异常是由this piece of code 上升的,这基本上是在测试所有可用的适配器,看起来最接近我想要做的是@987654322 @ 测试传递的字符串是否是文件。
发现这个我很惊讶,我想我可能在这里搞错了。如果我将初始化程序行交换为:
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"
【问题讨论】:
-
'has_attached_file :image' 代码在哪里?
-
把图片放到 /public/images/* 中,然后让你的 has_attached_file: default_url: "/image/*" 这很奇怪,如果没有指定附件,你会保存默认附件吗?
-
不,我放的*你放的文件名。
-
你的测试看起来怎么样@dongiulio
-
明白了!如果您希望模型使用 :default_url,请不要在参数中发送
:image。从参数中删除image,让我们看看它是怎么回事`\
标签: ruby-on-rails image paperclip