您好我正在尝试为我的Rails项目设置carrierwave。我看了其他答案,但似乎没有任何效果。当我尝试创建一个图像时,它给了我这个:
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和自制软件。有人可以帮忙吗?
BookCoverImage模型:
class BookCoverImage < Image
default_scope { where(group: self.name) }
belongs_to :book
mount_uploader :file, BookCoverImageUploader
end
它继承自Image:
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
My BookCoverImageUploader:
# 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
答案 0 :(得分:3)
如果File Failed to manipulate with MiniMagick, maybe it is not an image? Original Error: MiniMagick::Invalid
未能将ImageMagick
给定文件作为图片,则会引发错误identify
。
在命令提示符下尝试:(替换为您的文件名)
> 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.