【问题标题】:fixture_file_upload gives a "{***} has an extension that does not match its contents" from paperclip model rails 4.2.1fixture_file_upload 给出了来自回形针模型 rails 4.2.1 的“{***} 的扩展名与其内容不匹配”
【发布时间】:2023-03-06 08:48:01
【问题描述】:

我在使用 rails fixture_file_upload 上传回形针文档时遇到了这个问题(升级 rails 版本后)

知道回形针 4.0 及更高版本需要验证 content_type,我的规格仍然失败并出现错误:File has an extension that does not match its contents 即使在验证之后或特别请求 do_not_validate_attachment_file_type 之后。

我在多个(我所有的)回形针模型中都有这种情况......

一个例子如下:

型号:

class SignatureFile < ActiveRecord::Base
  ...
  has_attached_file :file
  VALID_CONTENT_TYPES = ["image/jpeg", "image/jpg", "image/png", "image/gif"]

  validates_attachment :file, content_type: { content_type: VALID_CONTENT_TYPES },
                              convert_options: { all: '-auto-orient' },
                              processors: [:compression]
  ...
end

规格:

image = fixture_file_upload('spec/fixtures/mobile_api/sample.jpg', 'image/jpg')

当我尝试在上面保存image 时,它失败了,image.errors.full_messages 给出:

“文件的扩展名与其内容不匹配”

我只想说我有很多像这样的场景,这些场景在升级之前已经通过了。

以下是我拥有的相关 gem 的版本:

  • gem 'rails', '4.2.7.1'
  • gem '回形针','4.2.1'
  • gem '回形针压缩','0.3.7'
  • gem 'rspec-rails', '3.4.2'

我会在这里遗漏什么?

【问题讨论】:

    标签: ruby-on-rails rspec paperclip


    【解决方案1】:

    您可能缺少file,遗憾的是,在这些情况下错误并不更精确。阅读paperclip docs

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-06
    相关资源
    最近更新 更多