Rails - Sidekiq错误未初始化常量

时间:2013-12-23 01:28:52

标签: ruby-on-rails ruby sidekiq

任何人都可以告诉我为什么我从Sidekiq收到以下错误:

NameError: uninitialized constant ImageWorker::DIRECT_UPLOAD_URL_FORMAT

的应用程序/模型/ choice.rb

class Choice < ActiveRecord::Base

 #Environment-specific direct upload url verifier screens for malicious posted upload locations.
  DIRECT_UPLOAD_URL_FORMAT = %r{\Ahttps:\/\/s3-us-west-1\.amazonaws\.com\/mybucket\/(?<path>uploads\/.+\/(?<filename>.+))\z}.freeze

  after_commit :queue_processing

  protected
  # Queue file processing
    def queue_processing
      ImageWorker.perform_async(id)
    end
end

的应用程序/工人/ image_worker.rb

 class ImageWorker
   include Sidekiq::Worker

   def perform(id)
     choice = Choice.find(id)
     direct_upload_url_data = DIRECT_UPLOAD_URL_FORMAT.match(choice.direct_upload_url)
     s3 = AWS::S3.new

     if choice.post_process_required?
       choice.picture = URI.parse(URI.escape(choice.direct_upload_url))
     else
       paperclip_file_path = "documents/uploads/#{id}/original/#{direct_upload_url_data[:filename]}"
    s3.buckets[Rails.configuration.aws[:bucket]].objects[paperclip_file_path].copy_from(direct_upload_url_data[:path])
  end

  choice.processed = true
  choice.save

  s3.buckets[Rails.configuration.aws[:bucket]].objects[direct_upload_url_data[:path]].delete
  end
 end

任何帮助都会很棒!

1 个答案:

答案 0 :(得分:2)

您应以下行移至ImageWorker类:

DIRECT_UPLOAD_URL_FORMAT = %r{\Ahttps:\/\/s3-us-west-1\.amazonaws\.com\/mybucket\/(?<path>uploads\/.+\/(?<filename>.+))\z}.freeze

或者,否则,请使用常量的完整限定路径:DIRECT_UPLOAD_URL_FORMAT,如下所示:

direct_upload_url_data = Choice::DIRECT_UPLOAD_URL_FORMAT.match(choice.direct_upload_url)

目前,您的代码正在尝试在DIRECT_UPLOAD_URL_FORMAT类中找到一个名为ImageWorker的常量,这个常量绝对没有初始化,因此也就是错误。