嗨,我正在尝试为我的 Rails 项目设置载波。我看过其他答案,但似乎没有任何效果。当我尝试创建图像时,它会给我这个:
1.9.3p392 :003 > bi = BookCoverImage.new
=> #<BookCoverImage id: nil, file: nil, created_at: nil, updated_at: nil, book_id: nil, category_id: nil, collection_id: nil, creator_id: nil, group: "BookCoverImage", homepage_slide_id: nil>
1.9.3p392 :004 > bi.file = File.open("#{Rails.root}/spec/support/sample_img.jpg")
=> #<File:/Users/emai/Documents/mysite/spec/support/sample_img.jpg>
1.9.3p392 :005 > bi.save
(0.1ms) SAVEPOINT active_record_1
(0.1ms) ROLLBACK TO SAVEPOINT active_record_1
=> false
1.9.3p392 :006 > bi.valid?
=> false
1.9.3p392 :007 > bi.errors.full_messages
=> ["File Failed to manipulate with MiniMagick, maybe it is not an image? Original Error: MiniMagick::Invalid"]
我正在使用Ruby 1.9.3,Mac Mavericks和自制软件。谁能帮忙?
书籍封面图像模型:
class BookCoverImage < Image
default_scope { where(group: self.name) }
belongs_to :book
mount_uploader :file, BookCoverImageUploader
end
它继承自图像:
class Image < ActiveRecord::Base
attr_accessible :file
after_initialize :set_group
before_save :set_group
private
def set_group
self.group = self.class.name
end
end
我的书封面图片上传器:
# encoding: utf-8
class BookCoverImageUploader < CarrierWave::Uploader::Base
# Include RMagick or MiniMagick support:
# include CarrierWave::RMagick
include CarrierWave::MiniMagick
# Choose what kind of storage to use for this uploader:
storage :file
# storage :fog
# Override the directory where uploaded files will be stored.
# This is a sensible default for uploaders that are meant to be mounted:
def store_dir
if Rails.env.test? || Rails.env.cucumber?
"#{Rails.root}/public/test/file_uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
else
"uploads/#{model.class.to_s.underscore}/#{model.id}"
end
end
# Provide a default URL as a default if there hasn't been a file uploaded:
# def default_url
# # For Rails 3.1+ asset pipeline compatibility:
# # ActionController::Base.helpers.asset_path("fallback/" + [version_name, "default.png"].compact.join('_'))
#
# "/images/fallback/" + [version_name, "default.png"].compact.join('_')
# end
# Process files as they are uploaded:
# process :scale => [200, 300]
#
# def scale(width, height)
# # do something
# end
# Create different versions of your uploaded files:
version :thumb do
process :resize_to_fit => [80, 10000]
end
version :medium do
process :resize_to_fit => [159, 10000]
end
version :main do
process :resize_to_fit => [238, 10000]
end
# Add a white list of extensions which are allowed to be uploaded.
# For images you might use something like this:
def extension_white_list
%w(jpg jpeg gif png)
end
# Override the filename of the uploaded files:
# Avoid using model.id or version_name here, see uploader/store.rb for details.
# def filename
# "something.jpg" if original_filename
# end
end
如果ImageMagick
无法将给定文件identify
为图像,则会引发错误File Failed to manipulate with MiniMagick, maybe it is not an image? Original Error: MiniMagick::Invalid
。
在命令提示符下尝试此操作:(替换为您的文件名)
> identify spec/support/facebook.jpg
### If image identified correctly, outputs as below
spec/support/facebook.jpg JPEG 400x267 400x267+0+0 8-bit sRGB 13.4KB 0.000u 0:00.000
> identify spec/support/test.png
### If image is invalid, outputs as below
identify: improper image header `spec/support/test.png' @ error/png.c/ReadPNGImage/4003.