gpt4 book ai didi

ruby-on-rails - 回形针在哪里放置丢失的.png 默认图像?

转载 作者:行者123 更新时间:2023-12-02 13:42:46 24 4
gpt4 key购买 nike

我在我的应用程序中使用回形针,但我的 Controller 测试失败,因为:

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 ,这基本上是测试所有可用的适配器,看起来最接近我想要做的适配器是 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"

最佳答案

对于这行代码:

Paperclip::Attachment.default_options[:default_url] = "/images/default_image.png"

Paperclip 正在寻找 /public/images/default_image.png 下的图像

更新:

所以你已经定义了样式bigthumb 。 Paperclip 将在 public/images/thumb/default_image.png 中查找 img或public/images/big/default_image.png取决于你会调用什么风格 <% image_tag @model.image.url(:style) %> .

更新#2(根据作者问题的更新#4)

明白了!如果您希望模型使用:default_url不要发送:image在参数中。从参数中删除图像,让我们看看效果如何`

关于ruby-on-rails - 回形针在哪里放置丢失的.png 默认图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25249252/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com