Rails 3 - Paperclip:未初始化的常量ActionDispatch :: Request :: UploadedFile

时间:2010-11-18 18:55:03

标签: ruby-on-rails paperclip

我有一个令人困惑的错误:

我使用Paperclip 2.3.5在Ruby 1.9.2上运行Rails 3.0.0。

当我上传文件时,我收到500错误。

NameError (uninitialized constant ActionDispatch::Request::UploadedFile):
  config/initializers/fix_params.rb:13:in `normalize_parameters'
  config/initializers/fix_params.rb:19:in `block in normalize_parameters'
  config/initializers/fix_params.rb:19:in `each'
  config/initializers/fix_params.rb:19:in `normalize_parameters'
  config/initializers/fix_params.rb:19:in `block in normalize_parameters'
  config/initializers/fix_params.rb:19:in `each'
  config/initializers/fix_params.rb:19:in `normalize_parameters'

Rendered /Users/vjmayr/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.0/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (10.2ms)

我已经看到类似的事情发生在Rails 3.0.2和.3但我不知道为什么会发生在这里。谁能帮助我找到问题?

谢谢!的Valentin

ADDED config / initializers / fix_params.rb:

module ActionController
  class Request
    private

      # Convert nested Hashs to HashWithIndifferentAccess and replace
      # file upload hashs with UploadedFile objects
      def normalize_parameters(value)
        case value
        when Hash
          if value.has_key?(:tempfile)
            upload = value[:tempfile]
            upload.extend(UploadedFile)
            upload.original_path = value[:filename]
            upload.content_type = value[:type]
            upload
          else
            h = {}
            value.each { |k, v| h[k] = normalize_parameters(v) }
            h.with_indifferent_access
          end
        when Array
          value.map { |e| normalize_parameters(e) }
       else
          value.force_encoding(Encoding::UTF_8) if value.respond_to?(:force_encoding)
          value
         end
      end
  end
end

1 个答案:

答案 0 :(得分:0)

tadman(谢谢!)指出我正确的解决方案: 我所要做的就是将tempfile中的值更改为to_tempfile。这可能不会发生在太多的人身上,但是我想我会提到,因为最近由于最近修改了稳定的导轨3.0.3,这是一种类似的讨论,这在一定程度上是完全正确的 - 但是已经放弃了有些人......

所以这是修改后的fix_params.rb片段

...
if value.has_key?(:to_tempfile)
        upload = value[:to_tempfile]
...

干杯,

缬氨酸