【问题标题】:Paperclip extension error回形针扩展错误
【发布时间】:2014-06-04 10:07:11
【问题描述】:

我使用this tutorial,但不能用回形针上传图片,它给出“头像的扩展名与其内容不匹配”错误。

我确信 ImageMagick 已安装并正常工作。

表格:

<%= form_for @user, :url => users_path, :html => { :multipart => true } do |f| %>
  <% if @user.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@user.errors.count, "error") %> prohibited this user from being saved:</h2>

      <ul>
      <% @user.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :avatar %><br>
    <%= f.file_field :avatar %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

用户控制器

class UsersController < ApplicationController
  before_action :set_user, only: [:show, :edit, :update, :destroy]
  # GET /users
  # GET /users.json
  def index
    @users = User.all
  end
  # GET /users/1
  # GET /users/1.json
  def show
  end
  # GET /users/new
  def new
    @user = User.new
  end
  # GET /users/1/edit
  def edit
  end
  # POST /users
  # POST /users.json
  def create
    @user = User.new(user_params)
    respond_to do |format|
      if @user.save
        format.html { redirect_to @user, notice: 'User was successfully created.' }
        format.json { render :show, status: :created, location: @user }
      else
        format.html { render :new }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
    end
  end
  # PATCH/PUT /users/1
  # PATCH/PUT /users/1.json
  def update
    respond_to do |format|
      if @user.update(user_params)
        format.html { redirect_to @user, notice: 'User was successfully updated.' }
        format.json { render :show, status: :ok, location: @user }
      else
        format.html { render :edit }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
    end
  end
  # DELETE /users/1
  # DELETE /users/1.json
  def destroy
    @user.destroy
    respond_to do |format|
      format.html { redirect_to users_url }
      format.json { head :no_content }
    end
  end
  private
    # Use callbacks to share common setup or constraints between actions.
    def set_user
      @user = User.find(params[:id])
    end
    # Never trust parameters from the scary internet, only allow the white list through.
    def user_params
      params.require(:user).permit(:avatar)
    end
end

user.rb

class User < ActiveRecord::Base
    has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png"
  validates_attachment_content_type :avatar, :content_type => /\Aimage\/.*\Z/
end

【问题讨论】:

  • 您要上传的文件是什么,它在哪里?你确定它的内容 do 匹配它的扩展名吗?
  • 我已经尝试过 avatar.png 和 avatar.jpg。我很确定这些图片。
  • 您在哪个平台上运行?什么版本的回形针?我相信 Paperclip 使用file 来检查content-type spoofing;如果你把你的头像图片放到服务器上,然后在上面运行file --mime-type avatar.jpg,结果是什么?
  • 我在 windows 7 上,'paperclip','~> 4.1.1',它给出错误:文件未被识别为内部或外部命令
  • 你肯定已经测试过ImageMagick的位置,并且设置了合适的环境文件的command_path,正如Paperclip的安装说明中描述的那样? (另外:作为健全性检查,您是否尝试过完全不同的文件?)

标签: ruby-on-rails ruby-on-rails-4 upload paperclip


【解决方案1】:

Paperclip 3.5.4 也适用于您的代码

【讨论】:

    【解决方案2】:

    我看到这是一个老问题,并且已经找到了最佳答案,但我最近遇到了同样的问题,并通过将 development.rb 中的原始 Paperclip.options 信息更新为:

    Paperclip.options[:command_path] = 'C:\Program Files\ImageMagick-6.9.0-Q16;C:\Program Files (x86)\GnuWin32\bin;'

    您必须将 ImageMagick 和 File 添加到路径中。省略对 File 的引用会产生原始引用的错误消息 "Avatar has an extension that does not match its contents",但省略 ImageMagick 会产生 Matt Gibson 提到的错误消息,"Paperclip::Errors::NotIdentifiedByImageMagickError."

    我不完全确定这是文件未安装或安装不正确的情况,而是没有人明确提到需要将两个引用都添加到 PATH 中。我知道对于已经编码一段时间的人来说,这似乎是一个相对“常识”类型的问题,但对于新手来说,有时需要说明这样的事情。我只是想通了,因为我使用 SUPER OLD 教程在我的机器上安装 Ruby/Rails 并且非常熟悉 PATH 和命令行,而较新的教程并没有详细介绍这类东西,而是想要让您“尽快启动并运行......”即使副作用是您不一定了解自己在做什么。

    【讨论】:

      【解决方案3】:

      我认为这里可能存在一些问题。首先,我认为您的“头像的扩展名与其内容不匹配”错误可能来自在 Windows 上运行而未安装 file。通过禁用 this answer 中的代码进行测试似乎可以解决该问题,但我认为您可能应该安装 Windows 版本的 file 并再次打开该欺骗检查。

      通过那个问题,你发现Paperclip::Errors::NotIdentifiedByImageMagickError出来了;我认为这是因为当您安装了 ImageMagick 时,您仍然需要在 Rails 环境中设置 Paperclip 的 command_path 配置选项,以便它可以找到它。

      【讨论】:

      • 是的,你是对的,当我从初始化程序中删除这些代码时,它不再起作用。如何安装文件? (我找不到!)
      • 我不确定;我不在 Windows 上。 This is the version linked to 来自Jon Yurek's article
      • 我无法正确安装文件,我最终使用了回形针 3.5.4,它工作正常。
      【解决方案4】:

      创建方法

          def create
          @user = User.new(params[:user])
      
          respond_to do |format|
            if @user.save
              format.html {
                render :json => [@user.to_jq_user].to_json,
                :content_type => 'text/html',
                :layout => false
              }
              format.json { render json: {files: [@user.to_jq_user]}, status: :created, location: @user }
            else
              format.html { render action: "new" }
              format.json { render json: @user.errors, status: :unprocessable_entity }
            end
           end
          end
      

      用户.rb

      class Upload < ActiveRecord::Base
        attr_accessible :user
        has_attached_file :user
        validates_attachment_content_type :user, :content_type => ["image/jpg", "image/jpeg", "image/png"]
        include Rails.application.routes.url_helpers
      
        def to_jq_user
          {
            "name" => read_attribute(:user_file_name),
            "size" => read_attribute(:user_file_size),
            "url" => user.url(:original),
            "delete_url" => user_path(self),
            "delete_type" => "DELETE" 
          }
        end
      end
      

      【讨论】:

      • 删除这行可以试试吗?
      • 它给了我“Paperclip::Errors::MissingRequiredValidatorError”错误
      • 尝试用do_not_validate_attachment_file_type :avatar替换该行并说出结果..
      • 初始错误:头像的扩展名与其内容不匹配
      • 我正在使用 rails 4, attr_accessible :user is for rails 3
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-27
      • 2012-07-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多