ruby-on-rails - 回形针: "has an extension that does not match its content"错误

标签 ruby-on-rails paperclip

我只是想用回形针上传一些非常简单的图片。我用谷歌搜索了这个问题,似乎其他人都有比简单上传更复杂的问题。以下是我的模型和 Controller 。

pin.rb

class Pin < ActiveRecord::Base

belongs_to :user
has_attached_file :image
validates_attachment_content_type :image, :content_type => /\Aimage\/.*\Z/


# validates the description
validates :description, presence: true 
validates :user_id, presence: true
# validates paperclip
validates_attachment :image, presence: true, 
                    content_type: { content_type: ["image/jpeg", "image/jpg", "image/png", "image/gif"]},
                    size: { less_than: 5.megabytes }


end

pin_controller.rb

 class PinsController < ApplicationController
    #before_filer will authentiate users to make sure they are logged in before doing anything with the Pins, with the except of indexing the pins so non-logged on users can also see them  
      before_filter :authenticate_user!, except: [:index]
      before_action :set_pin, only: [:show, :edit, :update, :destroy]

      # GET /pins
      # GET /pins.json
      def index
    @pins = Pin.all
  end

  # GET /pins/1
  # GET /pins/1.json
  def show
  end

  # GET /pins/new
  def new
    # associate @pin to the current user's id
    @pin = current_user.pins.new
  end

  # GET /pins/1/edit
  def edit
    # makes sure no other user can mess without a proper user id
    @pin = current_user.pins.find(params[:id])
  end

  # POST /pins
  # POST /pins.json
  def create
    # associate @pin to current user's id
    @pin = current_user.pins.new(pin_params)

    respond_to do |format|
      if @pin.save
        format.html { redirect_to @pin, notice: 'Pin was successfully created.' }
        format.json { render action: 'show', status: :created, location: @pin }
      else
        format.html { render action: 'new' }
        format.json { render json: @pin.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /pins/1
  # PATCH/PUT /pins/1.json
  def update
    # makes sure no other user can mess without a proper user id
    @pin = current_user.pins.find(params[:id])
    respond_to do |format|
      if @pin.update(pin_params)
        format.html { redirect_to @pin, notice: 'Pin was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @pin.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /pins/1
  # DELETE /pins/1.json
  def destroy
    # makes sure no other user can mess without a proper user id
    @pin = current_user.pins.find(params[:id])
    @pin.destroy
    respond_to do |format|
      format.html { redirect_to pins_url }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_pin
      @pin = Pin.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def pin_params
      params.require(:pin).permit(:description, :image)
    end
end

以下是我认为我的代码可能存在的问题。

首先,我遵循了一个月的 Rails 教程,但由于我正在观看过时的 Rails 3 系列,因此我必须自己通过研究将他教授的所有内容转换为 Rails 4。我可能在 pin.rb Controller 中设置了错误的强参数。我将 :image 属性字段添加到 pin_params 方法中,如下所示

     params.require(:pin).permit(:description, :image)

这是使用强参数添加 :image 的正确方法吗?在rails 3中,他在attr_accessible

中添加了:image

第二,在回形针的自述文件中,它说我应该运行which Convert并最终在我的配置文件中输入这一行

Paperclip.options[:command_path] = "/usr/local/bin/"

由于我使用的是Windows机器,这就是我在寻找答案几个小时后最终得到的结果

Paperclip.options[:command_path] = "C:/Program Files/ImageMagick-6.8.9-Q16/"

上面的路径对于 Windows 来说看起来相对正确吗?

最佳答案

我也使用 Windows 并收到此错误。我找到了答案here 。据我所知,Paperclip 通过让服务器操作系统验证文件的 MIME 类型来防止欺骗。它通过执行以下命令来做到这一点:

file -b --mime <my_file_name.extension>

问题是,标准 Windows 命令行没有 file 命令,因此欺骗检查失败,Paperclip 会抛出您观察到的错误。

要解决此问题:

  1. 安装 File for Windows .

  2. 编辑系统的 PATH 变量,使其包含“File for Windows”bin 目录路径。 (对我来说,它是C:\Program Files (x86)\GnuWin32\bin。)

  3. 重新启动命令行、Git Bash 或其他因编辑 PATH 变量而执行的操作。

  4. 确认 Windows 命令行现在响应 file 命令。

此方法在 Windows 8.1 和 Paperclip 4.2.1 上适用于我。

关于ruby-on-rails - 回形针: "has an extension that does not match its content"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24316149/

相关文章:

ruby-on-rails - Rails/Paperclip - 跳过图像处理

ruby-on-rails - 链接现有的S3文件作为回形针附件

ruby-on-rails - 如何在保存多态对象之前获取其父对象?

ruby-on-rails - Rails activerecord LIKE AND 子句错误

ruby-on-rails - 在 macOS 上安装 rmagick 不起作用,未找到重复引用和引用

ruby-on-rails - 回形针:继续编辑

ruby-on-rails - Rails Paper Clip 上传不起作用...没有抛出错误,回滚事务

ruby-on-rails - 无方法错误 : undefined method new for nil:NilClass

ruby-on-rails - Chartkick 在 localhost 上工作,但在 heroku 上显示 "loading"

ruby-on-rails - rails 设计 Hook 到 on_login