【问题标题】:Paperclip: "has an extension that does not match its content" error回形针:“具有与其内容不匹配的扩展名”错误
【发布时间】:2014-06-19 21:01:25
【问题描述】:

我只是想用回形针上传一些非常简单的图片。我用谷歌搜索了这个问题,似乎其他人的问题比简单的上传要复杂得多。下面是我的模型和控制器。

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 控制器中设置了错误的强参数。我像这样在 pin_params 方法中添加了 :image 属性字段

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

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

中添加了 :image

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

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

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

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

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

【问题讨论】:

    标签: ruby-on-rails paperclip


    【解决方案1】:

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

    file -b --mime <my_file_name.extension>
    

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

    要解决此问题:

    1. 安装File for Windows

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

    3. 重新启动命令行、Git Bash 或其他工具,因为您编辑了 PATH 变量。

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

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

    【讨论】:

      【解决方案2】:

      在你的 pin.rb 中删除这一行

      validates_attachment :image, presence: true, 
                      content_type: { content_type: ["image/jpeg", "image/jpg", "image/png", "image/gif"]},
                      size: { less_than: 5.megabytes }
      

      因为你已经使用了

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

      我还用过时的视频制作 One More Rails,就像你一样。 检查我的项目中的错误 这是链接My GigHub Project Files 祝你好运!)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-03-06
        • 1970-01-01
        • 2014-06-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多