2014-03-04 17 views
0

您好我正在嘗試爲我的Rails項目設置carrierwave。我看過其他答案,但似乎沒有任何工作。當我嘗試創建它給了我這樣一個形象:Rails 3 + Carrierwave:File無法使用MiniMagick進行操作,也許它不是圖像?原始錯誤:MiniMagick ::無效

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的小牛和自制。誰能幫忙?

BookCoverImage型號:

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 

我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

可以共享模型'BookCoverImage'和上傳。 –

+0

我用你要求的信息更新了這個問題 – Edmund

+0

Kirti我明白了。我在這裏重新安裝使用雨果的答案:http://stackoverflow.com/questions/9960683/mini-magick-gem-doesnt-work-with-my-imagemagick-install – Edmund

回答

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. 
相關問題