加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 将Factory_girl与PaperClip 4.0一起使用

发布时间:2020-12-16 19:03:22 所属栏目:百科 来源:网络整理
导读:有没有人知道使用factory_girl创建PaperClip 4.0附件的正确方法,绕过任何PaperClip处理和验证? 我曾经只能在我的工厂做以下事情: factory :attachment do supporting_documentation_file_name { 'test.pdf' } supporting_documentation_content_type { 'ap
有没有人知道使用factory_girl创建PaperClip 4.0附件的正确方法,绕过任何PaperClip处理和验证?

我曾经只能在我的工厂做以下事情:

factory :attachment do
  supporting_documentation_file_name { 'test.pdf' }
  supporting_documentation_content_type { 'application/pdf' }
  supporting_documentation_file_size { 1024 }
  # ...
end

这基本上会让PaperClip认为有一个有效的附件.

从3.5.3升级到4.0后,我现在收到验证错误:

ActiveRecord::RecordInvalid: Validation failed: Image translation missing: en.activerecord.errors.models.attachment.attributes.supporting_documentation.spoofed_media_type

注意:PaperClip 3.X的原始讨论在这里:How Do I Use Factory Girl To Generate A Paperclip Attachment?

解决方法

该问题似乎是由 line 61 in media_type_spoof_detector引起的.

Paperclip正在尝试查找您上传的“文件”的mime类型.如果没有,则验证失败,以保护您免受文件类型欺骗.

我自己没试过,但也许你最好的选择是使用一个真实的文件,并使用ActionDispatch :: TestProcess中的fixture_file_upload方法设置它.

factory :attachment do
   supporting_documentation { fixture_file_upload 'test.pdf','application/pdf' }

   # This is to prevent Errno::EMFILE: Too many open files
   after_create do |attachment,proxy|
     proxy.supporting_documentation.close
   end
end

您需要在test_helper.rb中包含ActionDispatch :: TestProcess

这是第一次发布here.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读