简体   繁体   English

无法使用paperclip gem将图像上传到heroku

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

I am trying to upload an image using my rails app. 我正在尝试使用我的rails应用程序上传图像。 I can upload an image via localhost. 我可以通过localhost上传图片。 But when i try on heroku I get a number of errors according to my heroku logs. 但是当我尝试使用heroku时,根据我的heroku日志会出现一些错误。 i get Paperclip::Errors::MissingRequiredValidatorError (Paperclip::Errors::MissingRequiredValidatorError): 我得到Paperclip :: Errors :: MissingRequiredValidatorError(Paperclip :: Errors :: MissingRequiredValidatorError):

but checked my listings.rb file and it shows the validation code 但检查了我的listing.rb文件,它显示了验证码

    class Listing < ActiveRecord::Base
        if Rails.env.development?
            has_attached_file :image, :styles => { :medium => "200x", :thumb => "100x100>" }, :default_url => "missing.jpg"
            validates_attachment_content_type :image, content_type: %w(image/jpeg image/jpg image/png)
        else
        has_attached_file :image, :styles => { :medium => "200x", :thumb => "100x100>" }, :default_url => "missing.jpg",
                          :storage => :dropbox,
                          :dropbox_credentials => Rails.root.join("config/dropbox.yml"),
                          :path => ":style/id_:filename"
        end 
    end

the rest of the logs is as follows 

    eb.1]: Started POST "/listings" for 92.5.120.49 at 2014-03-11 19:22:08 +0000
    2014-03-11T19:22:08.841367+00:00 app[web.1]: Started POST "/listings" for 92.5.120.49 at 2014-03-11 19:22:08 +0000
    2014-03-11T19:22:08.844182+00:00 app[web.1]:   Parameters: {"utf8"=>"✓", "authenticity_token"=>"C/PBKkFQp0mnhCF7AKNHB6aI7xuXk6qugZRzJXzay7Y=", "listing"=>{"name"=>"new", "description"=>"test", "price"=>"123", "image"=>#<ActionDispatch::Http::UploadedFile:0x007f962c1a5bc0 @tempfile=#<Tempfile:/tmp/RackMultipart20140311-2-1kxbxfd>, @original_filename="skillshareimage.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"listing[image]\"; filename=\"skillshareimage.jpg\"\r\nContent-Type: image/jpeg\r\n">}, "commit"=>"Create Listing"}
    2014-03-11T19:22:08.844182+00:00 app[web.1]:   Parameters: {"utf8"=>"✓", "authenticity_token"=>"C/PBKkFQp0mnhCF7AKNHB6aI7xuXk6qugZRzJXzay7Y=", "listing"=>{"name"=>"new", "description"=>"test", "price"=>"123", "image"=>#<ActionDispatch::Http::UploadedFile:0x007f962c1a5bc0 @tempfile=#<Tempfile:/tmp/RackMultipart20140311-2-1kxbxfd>, @original_filename="skillshareimage.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"listing[image]\"; filename=\"skillshareimage.jpg\"\r\nContent-Type: image/jpeg\r\n">}, "commit"=>"Create Listing"}
    2014-03-11T19:22:08.843929+00:00 app[web.1]: Processing by ListingsController#create as HTML
    2014-03-11T19:22:08.843929+00:00 app[web.1]: Processing by ListingsController#create as HTML
    2014-03-11T19:22:08.849897+00:00 app[web.1]: 
    2014-03-11T19:22:08.849897+00:00 app[web.1]: 
    2014-03-11T19:22:08.847858+00:00 app[web.1]: Completed 500 Internal Server Error in 3ms
    2014-03-11T19:22:08.849897+00:00 app[web.1]:   app/controllers/listings_controller.rb:27:in `create'
    2014-03-11T19:22:08.849897+00:00 app[web.1]: 
    2014-03-11T19:22:08.847858+00:00 app[web.1]: Completed 500 Internal Server Error in 3ms
    2014-03-11T19:22:08.849897+00:00 app[web.1]: 
    2014-03-11T19:22:08.849897+00:00 app[web.1]:   app/controllers/listings_controller.rb:27:in `create'
    2014-03-11T19:22:08.849897+00:00 app[web.1]: 
    2014-03-11T19:22:08.849897+00:00 app[web.1]: 
    2014-03-11T19:22:08.849897+00:00 app[web.1]: Paperclip::Errors::MissingRequiredValidatorError (Paperclip::Errors::MissingRequiredValidatorError):
    2014-03-11T19:22:08.849897+00:00 app[web.1]: Paperclip::Errors::MissingRequiredValidatorError (Paperclip::Errors::MissingRequiredValidatorError):
    2014-03-11T19:22:08.854943+00:00 heroku[router]: at=info method=POST path=/listings host=ancient-stream-6152.herokuapp.com request_id=b7f4b2fa-178c-4429-93d7-01a3cc2e9e12 fwd="92.5.120.49" dyno=web.1 connect=2ms service=12797ms status=500 bytes=1543
    2014-03-11T19:24:39.187964+00:00 heroku[router]: at=info method=GET path=/listings host=ancient-stream-6152.herokuapp.com request_id=c91e444c-f7f1-418f-a8de-963bc67c5c9b fwd="92.5.120.49" dyno=web.1 connect=1ms service=14ms status=200 bytes=2407
    2014-03-11T19:24:39.175939+00:00 app[web.1]: Started GET "/listings" for 92.5.120.49 at 2014-03-11 19:24:39 +0000
    2014-03-11T19:24:39.175939+00:00 app[web.1]: Started GET "/listings" for 92.5.120.49 at 2014-03-11 19:24:39 +0000
    2014-03-11T19:24:39.177622+00:00 app[web.1]: Processing by ListingsController#index as HTML
    2014-03-11T19:24:39.177622+00:00 app[web.1]: Processing by ListingsController#index as HTML
    2014-03-11T19:24:39.182852+00:00 app[web.1]:   Rendered listings/index.html.erb within layouts/application (3.2ms)
    2014-03-11T19:24:39.182852+00:00 app[web.1]:   Rendered listings/index.html.erb within layouts/application (3.2ms)
    2014-03-11T19:24:39.183742+00:00 app[web.1]:   Rendered layouts/_header.html.erb (0.1ms)
    2014-03-11T19:24:39.183742+00:00 app[web.1]:   Rendered layouts/_header.html.erb (0.1ms)
    2014-03-11T19:24:39.183978+00:00 app[web.1]:   Rendered layouts/_footer.html.erb (0.0ms)
    2014-03-11T19:24:39.183978+00:00 app[web.1]:   Rendered layouts/_footer.html.erb (0.0ms)
    2014-03-11T19:24:39.184228+00:00 app[web.1]: Completed 200 OK in 6ms (Views: 4.0ms | ActiveRecord: 1.6ms)
    2014-03-11T19:24:39.184228+00:00 app[web.1]: Completed 200 OK in 6ms (Views: 4.0ms | ActiveRecord: 1.6ms)
    2014-03-11T19:24:40.561315+00:00 app[web.1]: Started GET "/listings/new" for 92.5.120.49 at 2014-03-11 19:24:40 +0000
    2014-03-11T19:24:40.561315+00:00 app[web.1]: Started GET "/listings/new" for 92.5.120.49 at 2014-03-11 19:24:40 +0000
    2014-03-11T19:24:40.561315+00:00 app[web.1]: Processing by ListingsController#new as HTML
    2014-03-11T19:24:40.561315+00:00 app[web.1]: Processing by ListingsController#new as HTML
    2014-03-11T19:24:40.566669+00:00 app[web.1]:   Rendered listings/_form.html.erb (4.9ms)
    2014-03-11T19:24:40.567066+00:00 app[web.1]:   Rendered listings/_form.html.erb (4.9ms)
    2014-03-11T19:24:40.567066+00:00 app[web.1]:   Rendered listings/new.html.erb within layouts/application (5.4ms)
    2014-03-11T19:24:40.567066+00:00 app[web.1]:   Rendered listings/new.html.erb within layouts/application (5.4ms)
    2014-03-11T19:24:40.567637+00:00 app[web.1]:   Rendered layouts/_header.html.erb (0.0ms)
    2014-03-11T19:24:40.567637+00:00 app[web.1]:   Rendered layouts/_header.html.erb (0.0ms)
    2014-03-11T19:24:40.567880+00:00 app[web.1]:   Rendered layouts/_footer.html.erb (0.0ms)
    2014-03-11T19:24:40.567880+00:00 app[web.1]:   Rendered layouts/_footer.html.erb (0.0ms)
    2014-03-11T19:24:40.567998+00:00 app[web.1]: Completed 200 OK in 8ms (Views: 6.9ms | ActiveRecord: 0.0ms)
    2014-03-11T19:24:40.567998+00:00 app[web.1]: Completed 200 OK in 8ms (Views: 6.9ms | ActiveRecord: 0.0ms)
    2014-03-11T19:24:40.577742+00:00 heroku[router]: at=info method=GET path=/listings/new host=ancient-stream-6152.herokuapp.com request_id=3c8668c4-54ec-4b3f-a0b4-7dbf494af31c fwd="92.5.120.49" dyno=web.1 connect=2ms service=19ms status=304 bytes=811
    2014-03-11T19:25:04.428834+00:00 app[web.1]: Started POST "/listings" for 92.5.120.49 at 2014-03-11 19:25:04 +0000
    2014-03-11T19:25:04.440967+00:00 app[web.1]: Started POST "/listings" for 92.5.120.49 at 2014-03-11 19:25:04 +0000
    2014-03-11T19:25:04.451100+00:00 app[web.1]: Processing by ListingsController#create as HTML
    2014-03-11T19:25:04.457630+00:00 app[web.1]: Processing by ListingsController#create as HTML
    2014-03-11T19:25:04.457847+00:00 app[web.1]:   Parameters: {"utf8"=>"✓", "authenticity_token"=>"C/PBKkFQp0mnhCF7AKNHB6aI7xuXk6qugZRzJXzay7Y=", "listing"=>{"name"=>"", "description"=>"", "price"=>"", "image"=>#<ActionDispatch::Http::UploadedFile:0x007f962a7e8890 @tempfile=#<Tempfile:/tmp/RackMultipart20140311-2-hm3dpx>, @original_filename="newyork.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"listing[image]\"; filename=\"newyork.jpg\"\r\nContent-Type: image/jpeg\r\n">}, "commit"=>"Create Listing"}
    2014-03-11T19:25:04.457847+00:00 app[web.1]:   Parameters: {"utf8"=>"✓", "authenticity_token"=>"C/PBKkFQp0mnhCF7AKNHB6aI7xuXk6qugZRzJXzay7Y=", "listing"=>{"name"=>"", "description"=>"", "price"=>"", "image"=>#<ActionDispatch::Http::UploadedFile:0x007f962a7e8890 @tempfile=#<Tempfile:/tmp/RackMultipart20140311-2-hm3dpx>, @original_filename="newyork.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"listing[image]\"; filename=\"newyork.jpg\"\r\nContent-Type: image/jpeg\r\n">}, "commit"=>"Create Listing"}
    2014-03-11T19:25:04.463612+00:00 app[web.1]: Completed 500 Internal Server Error in 3ms
    2014-03-11T19:25:04.463612+00:00 app[web.1]: Completed 500 Internal Server Error in 3ms
    2014-03-11T19:25:04.467213+00:00 app[web.1]: 
    2014-03-11T19:25:04.467213+00:00 app[web.1]: Paperc
    lip::Errors::MissingRequiredValidatorError (Paperclip::Errors::MissingRequiredValidatorError):
    2014-03-11T19:25:04.467213+00:00 app[web.1]:   app/controllers/listings_controller.rb:27:in `create'
    2014-03-11T19:25:04.467213+00:00 app[web.1]: 
    2014-03-11T19:25:04.467213+00:00 app[web.1]: 
    2014-03-11T19:25:04.467687+00:00 app[web.1]: 
    2014-03-11T19:25:04.467687+00:00 app[web.1]: Paperclip::Errors::MissingRequiredValidatorError (Paperclip::Errors::MissingRequiredValidatorError):
    2014-03-11T19:25:04.467687+00:00 app[web.1]:   app/controllers/listings_controller.rb:27:in `create'
    2014-03-11T19:25:04.467687+00:00 app[w

listings controller.rb 列表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 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

Either remove the line 删除该行

if Rails.env.development?

from your Listing model or move the 从您的Listing模型或移动

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

out of the if statement. 超出if语句。

Validation of file type or content type is mandatory in Paperclip now unless you specifically say you don't want to do it. 现在,Paperclip必须验证文件类型或内容类型,除非您明确表示不想这样做。

Refer to https://github.com/thoughtbot/paperclip#security-validations for more information about validations. 有关验证的详细信息,请参阅https://github.com/thoughtbot/paperclip#security-validations

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

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