0

我正在使用Carrierwave -Gem 在我的网络应用程序中上传文件。但是由于以下错误,我无法保存图像:

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(主分支)

4

1 回答 1

0

我找到了解决方案。我必须先打电话给“ mount_uploader :image, ImageUploader”@!!!!

于 2012-06-01T21:01:37.687 回答