PaperClip和Rails 4:nil的未定义方法`split':NilClass

时间:2015-04-22 10:08:58

标签: ruby-on-rails ruby paperclip

这似乎是Rails 4的一个错误,它适用于Rails 3,但是4.1和4.2都失败了。我创建了一个新的测试项目,使用单个模型Post,然后添加了一个Paperclip文件字段,称为avatar。顺便提一下Paperclip 4.2.1。

这是我的测试模型,它只验证内容类型。

class Post < ActiveRecord::Base
  has_attached_file :avatar

  validates_attachment_content_type :avatar, :content_type => ["image/jpg", "image/jpeg", "image/png", "image/gif"]
end

控制器

class PostsController < ApplicationController
  #...
  # Just as generated by Rails, except as below, where I added the avatar field:

    # Never trust parameters from the scary internet, only allow the white list through.
    def post_params
      params.require(:post).permit(:name, :body, :avatar)
    end
end

_form视图

<%= form_for(@post, :url => posts_path, :html => { :multipart => true }) do |f| %>
  ...Several lines generated by Rails
  <div class="field">
    <%= f.label :body %><br>
    <%= f.file_field :avatar %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

尝试上传图片会产生此错误:

NoMethodError (undefined method `split' for nil:NilClass):                                                 
  paperclip (4.2.1) lib/paperclip/media_type_spoof_detector.rb:63:in `type_from_file_command'              
  paperclip (4.2.1) lib/paperclip/media_type_spoof_detector.rb:50:in `calculated_content_type'             
  paperclip (4.2.1) lib/paperclip/media_type_spoof_detector.rb:42:in `calculated_media_type'               
  paperclip (4.2.1) lib/paperclip/media_type_spoof_detector.rb:30:in `media_type_mismatch?'                
  paperclip (4.2.1) lib/paperclip/media_type_spoof_detector.rb:13:in `spoofed?'                            
  paperclip (4.2.1) lib/paperclip/validators/media_type_spoof_detection_validator.rb:8:in `validate_each'  
  activemodel (4.2.0) lib/active_model/validator.rb:151:in `validate'                                      
  org/jruby/RubyArray.java:1613:in `each'                                                                  
  activemodel (4.2.0) lib/active_model/validator.rb:148:in `validate'                                      
  paperclip (4.2.1) lib/paperclip/validators.rb:68:in `create_validating_before_filter'                        

我蠢蠢欲动。在... \ gems \ paperclip-4.2.1 \ lib \ paperclip \ helpers中,有一个使用此方法的模块:

def run(cmd, arguments = "", interpolation_values = {}, local_options = {})
  command_path = options[:command_path]
  Cocaine::CommandLine.path = [Cocaine::CommandLine.path, command_path].flatten.compact.uniq
  if logging? && (options[:log_command] || local_options[:log_command])
    local_options = local_options.merge(:logger => logger)
  end
  Cocaine::CommandLine.new(cmd, arguments, local_options).run(interpolation_values)
end

似乎偶然发现了最后一行,似乎是因为它正在寻找错误的文件。以下是参数:

Parameters: {"utf8"=>"?", "authenticity_token"=>"gYTVjpqNjHpzZt5aBdpWzHhwZ3V0URFhH/TkRQT5P6qJShmFuauehEdRXtGiNZoSmPmPK5q+F7jOv8VR3B6hTg==", "post"=>{"name"=>"name", "body"=>"body...", "avatar"=>#<ActionDispatch::Http::UploadedFile:0x86dbbe @original_filename="colour test.png", @headers="Content-Disposition: form-data; name=\"post[avatar]\"; filename=\"colour test.png\"\r\nContent-Type: image/png\r\n", @tempfile=#<Tempfile:C:\Users\user\AppData\Local\Temp\RackMultipart20150422-7116-12wd99n.png>, @content_type="image/png">}, "commit"=>"Create Post"}

请注意,临时文件启动“RackMultipart”(在Rails 4执行其强参数之后,它仍然存在)。然而,可卡因正在发送:

file -b --mime "C:\Users\user\AppData\Local\Temp\7f220723ebddeeb3a4feff6ec43c42a5150422-7116-1et5e2r-.png" 

RackMultipart文件存在,Concaine正在寻找的文件没有。毫不奇怪,它是对象,说“bin / rails:没有这样的文件或目录 - ”并返回nil,因此返回错误。

顺便说一下,我知道这个问题,但我认为这是一个不同的问题,因为这是Paperclip中的一个错误,显然在2012年解决了(尽管我在Windows 7上)。 Paperclip on Windows - undefined method `split' for nil:NilClass

0 个答案:

没有答案