如何使这个rails方法 - Windows兼容?

时间:2012-07-31 20:57:32

标签: windows ruby-on-rails-3 unix gem

我已经抓住了一个名为socialstream的gem的最新代码库,但我注意到Windows机器上的文件上传问题。问题似乎与下面的代码有关......

# Monkey patches to Ruby on Rails
#
# Use unix file util to prove the content type sent by the browser
class ActionDispatch::Http::UploadedFile
  def initialize_with_magic(*args, &block)
    initialize_without_magic(*args, &block)

    if (unix_file = `which file`.chomp).present? && File.exists?(unix_file)
      `#{ unix_file } -v 2>&1` =~ /^file-(.*)$/
      version = $1

      @content_type =
        if version >= "4.24"
          `#{ unix_file } -b --mime-type #{ @tempfile.path }`.chomp
        else
          `#{ unix_file } -bi #{ @tempfile.path }`.chomp =~ /(\w*\/[\w+-\.]*)/
          $1
        end
    end
  end

  alias_method_chain :initialize, :magic
end

错误如下

  

NoMethodError(未定义的方法chomp' for nil:NilClass): social_stream-base (0.22.0) lib/rails/social_stream.rb:8:in initialize_with_magic'     actionpack(3.2.5)lib / action_dispatch / http / upload.rb:39:in new' actionpack (3.2.5) lib/action_dispatch/http/upload.rb:39:in normalize_parameters'     actionpack(3.2.5)lib / action_dispatch / http / parameters.rb:73:in block in normalize_parameters' actionpack (3.2.5) lib/action_dispatch/http/parameters.rb:73:in each'     actionpack(3.2.5)lib / action_dispatch / http / parameters.rb:73:in normalize_parameters' actionpack (3.2.5) lib/action_dispatch/http/upload.rb:41:in normalize_parameters'     actionpack(3.2.5)lib / action_dispatch / http / parameters.rb:73:in block in normalize_parameters' actionpack (3.2.5) lib/action_dispatch/http/parameters.rb:73:in each'     actionpack(3.2.5)lib / action_dispatch / http / parameters.rb:73:in normalize_parameters' actionpack (3.2.5) lib/action_dispatch/http/upload.rb:41:in normalize_parameters'

有没有办法让这个工作在Windows上,通过使用gem,我可以在Windows上安装,或重写文件?

1 个答案:

答案 0 :(得分:1)

删除或注释掉该行:

alias_method_chain :initialize, :magic

这将阻止它挂钩到ActionDispatch :: HTTP :: UploadFile #initialize

这也意味着你不会得到“证明”内容类型的好处,但我不知道你的申请是否真的有必要。