本文介绍了确定内容类型时出现回形针错误:Rails 3.2.1 中的 Cocaine::CommandNotFoundError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用此处的代码上传和使用 Paperclip 裁剪图像.我可以上传图像并且图像也被裁剪但没有选择区域.控制台给出以下错误:

[paperclip] 确定内容类型时出错:Cocaine::CommandNotFoundError

我尝试过其他文章中给出的解决方案,例如 thisthis,但都没有解决这个问题.

欢迎提出任何其他建议.

注意:我在 Windows 7 上工作.安装的宝石是:

gem 'rails', '3.2.1'

宝石'mysql2'

宝石'回形针'

宝石 'mini_magick'

User.rb

需要 'mini_magick'类用户{:thumb =>"75x75#",:small =>"150x150>"}attr_accessible :name,:email,:avatar,:crop_x,:crop_y,:crop_w,:crop_hhas_attached_file :avatar,:styles =>{:thumb =>"75x75>",:small="100x100#", :large="500x500>"}#,:processors =>[:农作物]attr_accessor :processing, :crop_x,:crop_y,:crop_w,:crop_hafter_update :reprocess_avatar,:if=>:cropping?定义裁剪?!crop_x.blank?&&!crop_y.blank?&&!crop_w.blank?&&!crop_h.blank?结尾def avatar_geometry(style = :original)@geometry ||= {}@geometry[style] ||= Paperclip::Geometry.from_file(avatar.path(style))结尾私人的def reprocess_avatar# 如果用户没有更新照片,不要裁剪# ...或者如果照片已经在处理中返回除非(裁剪?&& !处理)self.processing = trueavatar.reprocess!self.processing = false结尾结尾

users_controller.rb

 def 创建@user = User.new(params[:user])如果@user.save如果参数[:user][:avatar].blank?flash[:notice] = "成功创建用户."重定向到@user别的渲染:动作=>庄稼"结尾别的渲染:动作=>'新的'结尾结尾# 放置/users/1# PUT/users/1.json定义更新@user = User.find(params[:id])如果@user.update_attributes(params[:user])如果参数[:user][:avatar].blank?flash[:notice] = "成功更新用户."重定向到@user别的渲染:动作=>庄稼"结尾别的渲染:动作=>'编辑'结尾结尾

crop.html.erb

<link rel="stylesheet" href="../stylesheets/jquery.Jcrop.css" type="text/css"/><link rel="stylesheet" href="../stylesheets/jquery.Jcrop.min.css" type="text/css"/><script src="../javascripts/jquery.min.js"></script><script src="../javascripts/jquery.Jcrop.js"></script><script type="text/javascript " charset="utf-8">$(函数(){$('#cropbox').Jcrop({onChange:update_crop,onSelect: update_crop,setSelect: [0, 0, 500, 500],纵横比:1});});功能更新作物(坐标){var rx = 100/coords.w;var ry = 100/coords.h;$('#preview').css({宽度:Math.round(rx * ) + 'px',高度:Math.round(ry * ) + 'px',marginLeft: '-' + Math.round(rx * coords.x) + 'px',marginTop: '-' + Math.round(ry * coords.y) + 'px'});var ratio = /;$("#crop_x").val(Math.round(coords.x * ratio));$("#crop_y").val(Math.round(coords.y * ratio));$("#crop_w").val(Math.round(coords.w * ratio));$("#crop_h").val(Math.round(coords.h * ratio));}<%结束%>裁剪框"%><h4>预览:</h4><div style="width:100px; height:100px; overflow:hidden">预览"%>

<% form_for @user do |f|%><% for attribute in [:crop_x, :crop_y, :crop_w, :crop_h] %>属性%><%结束%><p><%= f.submit "Crop"%></p><%结束%>

config/development.rb

 Paperclip.options[:swallow_stderr] = falsePaperclip.options[:command_path] = "C:/Program Files/ImageMagick-6.8.8-Q8/"
解决方案

这很可能是因为 Paperclip 4 使用 file 命令来确定文件的内容类型.该命令在普通 Windows 计算机上不可用.

下载 file 命令的 Win32 版本并将其放在搜索路径中的某个位置.(我使用的是 gnuwin32)或者您可以使用 3.x 版本的 Paperclip.

I tried to use the code given here to upload and crop images using Paperclip.I am able to upload image and also the image is cropped but without selection area. The console gives the following error :

[paperclip] Error while determining content type: Cocaine::CommandNotFoundError

I have tried solutions given on other articles of SO like this and this, but none of them solved this problem.

Any other suggestions are most welcomed.

NOTE : I am working on Windows 7.Gems installed are :

gem 'rails', '3.2.1'

gem 'mysql2'

gem 'paperclip'

gem 'mini_magick'

User.rb

require 'mini_magick'
class User < ActiveRecord::Base
#Paperclip.options[:command_path] = "C:\Program Files\ImageMagick-6.8.8-Q8"
#  The foll line works
#  has_attached_file :avatar,:styles => {:thumb => "75x75#",:small => "150x150>"} 

  attr_accessible :name,:email,:avatar,:crop_x,:crop_y,:crop_w,:crop_h
  has_attached_file :avatar,:styles => {:thumb => "75x75>",:small=>"100x100#", :large=>"500x500>"}#,:processors => [:cropper]
  attr_accessor :processing, :crop_x,:crop_y,:crop_w,:crop_h
  after_update :reprocess_avatar,:if=>:cropping?                       

  def cropping?
    !crop_x.blank? && !crop_y.blank? && !crop_w.blank? && !crop_h.blank?
  end

  def avatar_geometry(style = :original)
    @geometry ||= {}
    @geometry[style] ||= Paperclip::Geometry.from_file(avatar.path(style))
  end

  private

  def reprocess_avatar
    # don't crop if the user isn't updating the photo
    # ...or if the photo is already being processed
     return unless (cropping? && !processing)
     self.processing = true
     avatar.reprocess!
     self.processing = false
  end   

end

users_controller.rb

 def create
  @user = User.new(params[:user])
  if @user.save
    if params[:user][:avatar].blank?
      flash[:notice] = "Successfully created user."
      redirect_to @user
    else
      render :action => "crop"
    end
  else
    render :action => 'new'
  end
end


  # PUT /users/1
  # PUT /users/1.json
  def update
    @user = User.find(params[:id])
  if @user.update_attributes(params[:user])
    if params[:user][:avatar].blank?
      flash[:notice] = "Successfully updated user."
      redirect_to @user
    else
      render :action => "crop"
    end
  else
    render :action => 'edit'
  end

 end

crop.html.erb

<% content_for(:head) do %>

<link rel="stylesheet" href="../stylesheets/jquery.Jcrop.css" type="text/css" />
<link rel="stylesheet" href="../stylesheets/jquery.Jcrop.min.css" type="text/css" />
<script src="../javascripts/jquery.min.js"></script>
<script src="../javascripts/jquery.Jcrop.js"></script>

<script type="text/javascript " charset="utf-8">
$(function() {
  $('#cropbox').Jcrop({
    onChange: update_crop,
    onSelect: update_crop,
    setSelect: [0, 0, 500, 500],
    aspectRatio: 1
  });
});

function update_crop(coords) {
  var rx = 100/coords.w;
  var ry = 100/coords.h;
  $('#preview').css({
    width: Math.round(rx * <%= @user.avatar_geometry(:large).width %>) + 'px',
    height: Math.round(ry * <%= @user.avatar_geometry(:large).height %>) + 'px',
    marginLeft: '-' + Math.round(rx * coords.x) + 'px',
    marginTop: '-' + Math.round(ry * coords.y) + 'px'
  });
  var ratio = <%= @user.avatar_geometry(:original).width %> / <%= @user.avatar_geometry(:large).width %>;
  $("#crop_x").val(Math.round(coords.x * ratio));
  $("#crop_y").val(Math.round(coords.y * ratio));
  $("#crop_w").val(Math.round(coords.w * ratio));
  $("#crop_h").val(Math.round(coords.h * ratio));
}
</script>
<% end %>

<%= image_tag @user.avatar.url(:large), :id => "cropbox" %>

<h4>Preview:</h4>
<div style="width:100px; height:100px; overflow:hidden">
  <%= image_tag @user.avatar.url(:large), :id => "preview" %>
</div>

<% form_for @user do |f| %>
  <% for attribute in [:crop_x, :crop_y, :crop_w, :crop_h] %>
    <%= f.hidden_field attribute, :id => attribute %>
  <% end %>
  <p><%= f.submit "Crop" %></p>
<% end %>

config/development.rb

 Paperclip.options[:swallow_stderr] = false
  Paperclip.options[:command_path] = "C:/Program Files/ImageMagick-6.8.8-Q8/"
解决方案

This is most likely because Paperclip 4 uses the file command to determine the content type of a file. That command is not available on a normal Windows machine.

Either download a Win32 version of the file command and put it somewhere in the search path. (I'm using gnuwin32) Alternatively you can use the 3.x version of Paperclip.

这篇关于确定内容类型时出现回形针错误:Rails 3.2.1 中的 Cocaine::CommandNotFoundError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 18:48