Ruby on rails 无法使用回形针上载图像

Ruby on rails 无法使用回形针上载图像,ruby-on-rails,ruby,ruby-on-rails-4,Ruby On Rails,Ruby,Ruby On Rails 4,我得到这个错误: 回形针::错误::ListingsController中缺少RequiredValidator错误#创建 回形针::错误::缺少RequiredValidator错误 # POST /listings.json def create @listing = Listing.new(listing_params) respond_to do |format| if @listing.save app/controllers/listings_c

我得到这个错误:

回形针::错误::ListingsController中缺少RequiredValidator错误#创建 回形针::错误::缺少RequiredValidator错误

 # 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'

我的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
类列表{:medium=>“200x”,:thumb=>“100x100>”},
:default_url=>“default.jpg”,
:存储=>:dropbox,
:dropbox_credentials=>Rails.root.join(“config/dropbox.yml”)
结束
我的列表\u 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
class ListingsController

感谢您的帮助

这是rails 3的新回形针补丁 你必须加上-

验证附件内容类型:图像、内容类型=>['image/jpeg'、'image/jpg'、'image/png']

到使用回形针的模型。 您可以在本期讨论中阅读更多内容:

这是rails 3的新回形针补丁 你必须加上-

验证附件内容类型:图像、内容类型=>['image/jpeg'、'image/jpg'、'image/png']

到使用回形针的模型。 您可以在本期讨论中阅读更多内容:

这是rails 3的新回形针补丁 你必须加上-

验证附件内容类型:图像、内容类型=>['image/jpeg'、'image/jpg'、'image/png']

到使用回形针的模型。 您可以在本期讨论中阅读更多内容:

这是rails 3的新回形针补丁 你必须加上-

验证附件内容类型:图像、内容类型=>['image/jpeg'、'image/jpg'、'image/png']

到使用回形针的模型。 您可以在本期讨论中阅读更多内容:

为了补充@rossmari的答案(他是正确的),有一种更通用的方法来验证图像,而不仅仅是指定每种图像类型,这可能会变得相当麻烦,具体取决于您希望允许的图像类型

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

为了补充@rossmari的答案(他是正确的),有一种更通用的方法来验证图像,而不仅仅是指定每种图像类型,这可能会变得相当麻烦,具体取决于您希望允许的图像类型

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

为了补充@rossmari的答案(他是正确的),有一种更通用的方法来验证图像,而不仅仅是指定每种图像类型,这可能会变得相当麻烦,具体取决于您希望允许的图像类型

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

为了补充@rossmari的答案(他是正确的),有一种更通用的方法来验证图像,而不仅仅是指定每种图像类型,这可能会变得相当麻烦,具体取决于您希望允许的图像类型

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

我想这就是你要找的我想这就是你要找的我想这就是你要找的我想这就是你要找的