【问题标题】:Showing Content type invalid, while uploading photo in rails application using paperclip使用回形针在 Rails 应用程序中上传照片时显示内容类型无效
【发布时间】:2014-07-25 14:27:50
【问题描述】:

这是我的照片模型工厂

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

这是照片模型,其中图像是列的名称。我正在使用回形针宝石作为图像。

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

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

【问题讨论】:

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


    【解决方案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'
    

    【讨论】:

    • 现在显示此错误验证失败:图像无法运行identify 命令。请安装 ImageMagick。,Image 无法运行 identify 命令。请安装 ImageMagick。但是我没有使用 ImageMagick,所以我需要安装它吗?
    • 您需要安装 ImageMagick。你在 Mac 上吗?如果你有自制软件,你可以运行brew install imagemagick。或阅读:imagemagick.org/script/binary-releases.php
    猜你喜欢
    • 1970-01-01
    • 2013-11-09
    • 2011-01-03
    • 2011-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-26
    相关资源
    最近更新 更多