PaperClip and Rails 4: undefined `split 'method for nil: NilClass

This seems to be a bug with Rails 4, it worked with Rails 3 but doesn't work with both 4.1 and 4.2. I created a new test project with one Post model and then added a Paperclip file field called avatar. Paperclip 4.2.1, by the way.

Here is my test model that just checks the content type.

class Post < ActiveRecord::Base
  has_attached_file :avatar

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



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)


_Form image

<%= 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 class="actions">
    <%= f.submit %>
<% end %>


When trying to load an image, this error is generated:

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/ `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'                        


I had a dream. In ... \ gems \ paperclip-4.2.1 \ lib \ paperclip \ helpers there is a module with this method:

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, arguments, local_options).run(interpolation_values)


He seems to have stumbled upon this last line and it seems he is looking for the wrong file. Here are the parameters:

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"}


Note that the temp file starts "RackMultipart" (and this still exists after Rails 4 executes its strong options). However, Cocaine ships like this:

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


The RackMultipart file exists, the one that Concaine is looking for does not support. Not surprisingly, this indicates that "bin / rails: No such file or directory -" and returns nil and therefore an error.

By the way, I am aware of this question, but I believe this is a different issue as it was a bug in Paperclip that was apparently resolved in 2012 (although I am on Windows 7). Windows paperclip - undefined 'split' method for nil: NilClass

source to share

All Articles