【发布时间】:2014-02-06 18:22:08
【问题描述】:
我在我的 Rails 应用程序中使用Paperclip 来附加images。
我在我的模型中声明了对 content_type 的验证为
validates_attachment :image,
:content_type => { :content_type => ["image/jpg", "image/gif", "image/png"] }
我有两个示例,一个具有有效图像,另一个具有无效图像
对于无效图像,我只是将 .txt 文件重命名为 .png
it "Image is valid" do
image = File.new("#{Rails.root}/spec/support/right.png")
expect(FactoryGirl.build(:pin, image: image)).to be_valid
end
it "Image is invalid" do
image = File.new("#{Rails.root}/spec/support/wrong.png")
expect(FactoryGirl.build(:pin, image: image)).to have(1).errors_on(:image_content_type)
end
我希望我的两个示例都能成功运行。但是,我的第二个例子失败了。
我没有收到 wrong.png 的 content_type 的任何错误。
我认为 Paperclip 的 content_type 验证实际上会检查上传文件的文件格式(二进制数据编码)。但似乎在这里,它只是检查文件扩展名。此验证是否仅检查上传文件的扩展名?
我可能在这里遗漏了一些东西(配置?)。 Paperclip 中是否有任何其他可用的验证来实现这一点?还是在这种情况下我应该选择自定义验证器?
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-4 rspec paperclip