Paperclip:“有一个与其内容不匹配的扩展名”错误

时间:2014-06-19 21:01:26

标签: ruby-on-rails paperclip

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

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

以下是我认为我的代码可能出错的原因。

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

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

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

中添加了:image

第二次,在回形针的自述文件中,它说我应该运行转换并最终在我的配置文件中输入此行

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

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

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

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

2 个答案:

答案 0 :(得分:2)

我也使用Windows并且收到此错误。我找到了答案here。从我收集的信息中,Paperclip通过让服务器的操作系统验证文件的MIME类型来防止欺骗。它通过执行以下命令来执行此操作:

file -b --mime <my_file_name.extension>

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

要解决此问题:

  1. 安装File for Windows

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

  3. 重启您的命令行,Git Bash,或者因为您编辑了PATH变量而拥有的内容。

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

  5. 此方法适用于Windows 8.1 with Paperclip 4.2.1。

答案 1 :(得分:0)

在你的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/

我也像你一样用过时的视频做一个更多的Rails。 检查我的项目中的错误 这是链接My GigHub Project Files 祝你好运!)