0

I am getting this error:

Paperclip::Errors::MissingRequiredValidatorError in ListingsController#create Paperclip::Errors::MissingRequiredValidatorError

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

    respond_to do |format|
      if @listing.save

app/controllers/listings_controller.rb:27:in `create'

My listing.rb is

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

My 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

Thanks for the help

frankzk
  • 556
  • 2
  • 6
  • 14
  • I think this is what you are looking for http://stackoverflow.com/questions/21897725/papercliperrorsmissingrequiredvalidatorerror-with-rails-4 – Rafal Jul 03 '14 at 02:24

2 Answers2

2

its the new paperclip patch for rails 3 You have to add -

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

to your model where paperclip is used. you can read more in this issue discussion: https://github.com/galetahub/ckeditor/issues/399

rossmari
  • 81
  • 5
1

To add to @rossmari's answer (which he is correct), there is a more generic way to validate for images, rather than just specifying each image type, which can get pretty cumbersome depending on what type of images you would like to allow.

validates_attachment_content_type :image, content_type: /\Aimage\/.*\Z/
Justin
  • 4,922
  • 2
  • 27
  • 69