Rails 3.2.3 Carrierwave-Gem错误“私有方法`write_uploader'调用”

时间:2012-05-19 10:51:43

标签: upload ruby-on-rails-3.2 carrierwave

我在我的网络应用程序中使用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(主分支)

1 个答案:

答案 0 :(得分:0)

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