nowa / carrierwave-upyun

UpYun storage for CarrierWave
161 stars 27 forks source link

NoMethodError: undefined method `uploader_secure_token' #15

Closed yesmeck closed 4 years ago

yesmeck commented 12 years ago

uploader 就是按 readme 里写的。保存图片的时候会有这个错误,是否跟我是用 carrierwave 远程抓取图片的有关?

# encoding: utf-8
# app/uploaders/photo_uploader.rb

IMAGE_UPLOADER_ALLOW_IMAGE_VERSION_NAMES = %(:thumb :tiny)
class PhotoUploader < CarrierWave::Uploader::Base
  include CarrierWave::RMagick

  storage :upyun

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

  # Override url method to implement with "Image Space"
  def url(version_name = "")
    @url ||= super({})
    version_name = version_name.to_s
    return @url if version_name.blank?
    if not version_name.in?(IMAGE_UPLOADER_ALLOW_IMAGE_VERSION_NAMES)
      # To protected version name using, when it not defined, this will be give an error message in development environment
      raise "ImageUploader version_name:#{version_name} not allow."
    end
    [@url,version_name].join("!") # thumb split with "!"
  end

  def extension_white_list
    %w(jpg jpeg gif png)
  end

  def filename
    if super.present?
      model.uploader_secure_token ||= SecureRandom.uuid.gsub("-","")
      Rails.logger.debug("(BaseUploader.filename) #{model.uploader_secure_token}")
      "#{model.uploader_secure_token}.#{file.extension.downcase}"
    end
  end
end
# app/models/photo.rb
class Photo < ActiveRecord::Base
  mount_uploader :image, PhotoUploader

  attr_accessible :image, :remote_image_url

  belongs_to :imageable, :polymorphic => true
end
illnino commented 11 years ago

我有同樣問題 Ruby-China 的解答,對我無效,請問有解決方法麼?

gonjay commented 11 years ago

我也遇到了同样的问题,后来把filename给重写了,反正这个方法也只是为了返回一个文件名字符串好去图片空间访问,那么我们也能自己写一个名字的生成器来生成一个不会重复的文件名就可以了,联想到密码密文的生成方法,有人给出了这样的解决方案:

  def filename
    if super.present?
      @name ||="#{Digest::MD5.hexdigest(original_filename)}.#{file.extension.downcase}" if original_filename
    end
  end