使用回形针在rails应用程序中上传照片时显示内容类型无效

时间:2014-07-25 14:27:51

标签: ruby-on-rails-4 paperclip factory-bot

这是我的照片模型工厂

factory :photo do
user 
image Rack::Test::UploadedFile.new(File.join(Rails.root,'spec','GIV_jpg_file_small.jpg'))
end

这是照片模型,其中image是列的名称。我正在使用paperclip gem进行图像处理。

class Photo < ActiveRecord::Base
has_attached_file :image, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png"
validates_attachment :image, :presence => true
validates_attachment_content_type :image, :content_type => /\Aimage\/.*\Z/
validates_attachment_size :image, :less_than => 1.megabytes
belongs_to :user
end

现在,当我为这个照片模型运行Rspec时,它正在显示

Failure/Error: let(:photo) {FactoryGirl.create(:photo)}
ActiveRecord::RecordInvalid:
Validation failed: Image content type is invalid, Image is invalid

有人可以解决我的问题吗? 提前谢谢。

1 个答案:

答案 0 :(得分:1)

你错过了Rack::Test::UploadedFile.new的第二个参数。它需要知道content_type。只需添加'image / jpg':

image_path = File.join(Rails.root,'spec','GIV_jpg_file_small.jpg')
Rack::Test::UploadedFile.new image_path, 'image/jpg'