2012-05-19 4 views
0

Я использую Carrierwave -Gem для загрузки файлов в мое веб-приложение. Но это невозможно для меня, чтобы сохранить изображение из-за следующую ошибку:Rails 3.2.3 Ошибка Carrierwave-Gem "частный метод` write_uploader 'под названием «

private method `write_uploader' called for #<Image:0x000000035eafd0> 

Я Жду»нашел хорошо работающее решение, чтобы решить эту проблему. Вот мое приложение Код:

/app/models/image.rb

#encoding: utf-8 
class Image < ActiveRecord::Base 

    scope :public, joins(:gallery).where('images.public=true AND galleries.public=true') 

    belongs_to :gallery 
    attr_accessible :gallery_id, :name, :image, :remote_image_url, :desc, :public, :crop_x, :crop_y, :crop_w, :crop_h 
    attr_accessor :crop_x, :crop_y, :crop_w, :crop_h 
    mount_uploader :image, ImageUploader 

    validates :gallery_id, presence: true 
    after_update :crop_gallery_image 

    # this method is needed and a part of a quickhack because of an bug in caarierwave, if you want to save files in a private folder 
    def image_url(image_size) 
    Rails.application.routes.url_helpers.image_url(image_id: self.id, image_size:  image_size, host: (Rails.env.production? ? 'xyz.de': 'localhost:3000')) 
    end 

    def crop_gallery_image 
    image.recreate_versions! if crop_x.present? 
    end 

end 

/app/uploaders/image_uploader.rb

#encoding: utf-8 
class ImageUploader < CarrierWave::Uploader::Base 
    include CarrierWave::RMagick 

    storage :file 

    #the following methods doesn't work @ the moment; i used an initializer to change the paths 
    def store_dir 
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}" 
    end 

    def cache_dir 
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}" 
    end 


    version :thumb do 
    resize_to_limit(200, 200) 
    end 

    version :medium do 
    resize_to_limit(800, 600) 
    end 

    version :normal do 
    resize_to_limit(1024, 768) 
    end 

    version :big do 
    resize_to_limit(1440, 1024) 
    end 

    version :gallery do 
    process :crop 
    resize_to_fill(920, 400) 
    end 

    def crop 
    if model.crop_x.present? 
     resize_to_limit(1024, 768) 
     manipulate! do |img| 
     x = model.crop_x.to_i 
     y = model.crop_y.to_i 
     w = model.crop_w.to_i 
     h = model.crop_h.to_i 
     img.crop!(x, y, w, h) 
     end 
    end 
    end 
end 

Исключение вызывается после того, как я пытаюсь загрузить изображение. Я использую Ruby 1.9.3-p125, Rails 3.2.3 и фактический Carrierwave-Gem (master-branch)

ответ

0

Я нашел решение. Я должен позвонить «mount_uploader :image, ImageUploader» @ сначала !!!!

Смежные вопросы