简体   繁体   English

无法使用回形针gem上传图像

[英]unable to upload an image using the paperclip gem

having installed the latest version of paperclip , i get an error every time i try and upload an image , i can click on the upload button , select my image but when i try to create a listing/image i get error Paperclip::Errors::MissingRequiredValidatorError in ListingsController#create Paperclip::Errors::MissingRequiredValidatorError ..imagemagick installed as well 安装了最新版本的回形针后,每次尝试上传图像时都会出现错误,我可以单击“上载”按钮,选择我的图像,但是当我尝试创建列表/图像时,出现错误Paperclip :: Errors:还安装了ListingsController#create Paperclip :: Errors :: MissingRequiredValidatorError ..imagemagick的:MissingRequiredValidatorError

listings.controller.rb 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)  **<------ERROR** 

    respond_to do |format|
      if @listing.save
        format.html { redirect_to @listing, notice: 'Listing was successfully created.' }
        format.json { render action: 'show', status: :created, location: @listing }
      else
        format.html { render action: '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 { head :no_content }
      else
        format.html { render action: '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 }
      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

listing.rb listing.rb

class Listing < ActiveRecord::Base
  has_attached_file :image, :styles => { :medium => "200x", :thumb => "100x100>" }, :default_url => "missing.jpg"
end

_form.html.erb _form.html.erb

<%= form_for(@listing, :html => { :multipart => true }) do |f| %>
  <% if @listing.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@listing.errors.count, "error") %> prohibited this listing from being saved:</h2>

      <ul>
      <% @listing.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="form-group">
    <%= f.label :name %>
    <%= f.text_field :name, class: "form-control" %>
  </div>
  <div class="form-group">
    <%= f.label :description %>
    <%= f.text_area :description, class: "form-control" %>
  </div>
 <div class="form-group">
    <%= f.label :price %>
    <%= f.text_field :price, class: "form-control" %>
  </div>
  <div class="form-group">
    <%= f.label :image %>
    <%= f.file_field :image, class: "form-control" %>
  </div>
   <div class="form-group">
     <%= f.submit class: "btn btn-primary" %>
   </div>
<% end %>

您必须在模型中添加内容类型的验证:

validates_attachment_content_type :image, content_type: %w(image/jpeg image/jpg image/png)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM