无法使用回形针上传图像

时间:2014-07-03 02:22:29

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

我收到此错误:

在ClipController中创建了Paperclip :: Errors :: MissingRequiredValidatorError 纸夹::错误:: MissingRequiredValidatorError

 # POST /listings.json
  def create
    @listing = Listing.new(listing_params)

    respond_to do |format|
      if @listing.save

app / controllers / listings_controller.rb:27:在'create'

我的listing.rb是

class Listing < ActiveRecord::Base
            has_attached_file :image, 
      :styles => { :medium => "200x", :thumb => "100x100>"},
        :default_url => "default.jpg",
      :storage => :dropbox,
      :dropbox_credentials => Rails.root.join("config/dropbox.yml")
end

我的listings_controller.rb

class ListingsController < ApplicationController
  before_action :set_listing, only: [:show, :edit, :update, :destroy]

  # GET /listings
  # GET /listings.json
  def index
    @listings = Listing.all
  end

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

  # GET /listings/new
  def new
    @listing = Listing.new
  end

  # GET /listings/1/edit
  def edit
  end

  # POST /listings
  # POST /listings.json
  def create
    @listing = Listing.new(listing_params)

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

  # PATCH/PUT /listings/1
  # PATCH/PUT /listings/1.json
  def update
    respond_to do |format|
      if @listing.update(listing_params)
        format.html { redirect_to @listing, notice: 'Listing was successfully updated.' }
        format.json { render :show, status: :ok, location: @listing }
      else
        format.html { render :edit }
        format.json { render json: @listing.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /listings/1
  # DELETE /listings/1.json
  def destroy
    @listing.destroy
    respond_to do |format|
      format.html { redirect_to listings_url, notice: 'Listing was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

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

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

感谢您的帮助

2 个答案:

答案 0 :(得分:2)

它是用于rails 3的新回形针补丁 你必须添加 -

validates_attachment_content_type :image, :content_type => ['image/jpeg', 'image/jpg','image/png']

到使用回形针的模型。 您可以在本期讨论中阅读更多内容: https://github.com/galetahub/ckeditor/issues/399

答案 1 :(得分:1)

要添加到@ rossmari的答案(他是正确的),有一种更通用的方法来验证图像,而不是仅指定每种图像类型,这可能会非常麻烦,具体取决于您希望的图像类型允许。

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