簡體   English   中英

無法使用回形針保存圖像,軌道上的紅寶石

[英]Can't save image with paperclip, ruby on rails

我正在使用回形針上傳圖片。 我的應用程序可以正確導入圖像,但無法保存。 這是我的控制器:

def create
    @upload = Upload.new(params[:upload])
    logger.debug "image: #{@upload.image}"
    logger.debug "image: #{@upload.image_content_type}"
    logger.debug "image: #{@upload.image_file_name}"
    if @upload
      @upload.save
      redirect_to uploads_new_path
    else
      render json: { error: @upload.errors.full_messages.join(',')}, :status => 400
    end
  end

HTML:

<div class="medium-10 medium-centered row">
    <div class="medium-10 medium-centered columns">
        <%= form_for(@upload,{:action=>"create", :controller=>"uploads", :method => "post"}) do |f| %>
            <%= f.file_field :image %>
            <br>
            <%= f.submit "Upload" %>
        <% end %>
    </div>
</div>

當我檢查日志時:

Started POST "/uploads" for 127.0.0.1 at 2014-12-19 17:18:41 +0700
Processing by UploadsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"PzxL26+hLSamelcOvU/5C9zK+UeNatRLAeK8mWVKgPs=", "upload"=>{"image"=>#<ActionDispatch::Http::UploadedFile:0x3c19890 @original_filename="Capture.JPG", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"upload[image]\"; filename=\"Capture.JPG\"\r\nContent-Type: image/jpeg\r\n", @tempfile=#<File:C:/Users/QUANGD~1/AppData/Local/Temp/RackMultipart20141219-4880-1hzz874>>}, "commit"=>"Upload"}
Command :: file -b --mime "C:/Users/QUANGD~1/AppData/Local/Temp/44a50f07b4bdc57740901280f9eddaf520141219-4880-gh60e.JPG"
[paperclip] Content Type Spoof: Filename Capture.JPG (["image/jpeg"]), content type discovered from file command: . See documentation to allow this combination.
image: /system/uploads/images//original/Capture.JPG?1418984321
image: image/jpeg
image: Capture.JPG
  [1m[36m (0.0ms)[0m  [1mbegin transaction[0m
Command :: file -b --mime "C:/Users/QUANGD~1/AppData/Local/Temp/44a50f07b4bdc57740901280f9eddaf520141219-4880-nu1ety.JPG"
[paperclip] Content Type Spoof: Filename Capture.JPG (["image/jpeg"]), content type discovered from file command: . See documentation to allow this combination.
  [1m[35m (0.0ms)[0m  rollback transaction
Redirected to http://localhost:3000/uploads/new
Completed 302 Found in 430.0ms (ActiveRecord: 0.0ms)

我的應用仍然可以正確導入圖像:

image: /system/uploads/images//original/Capture.JPG?1418984321
image: image/jpeg
image: Capture.JPG

但是我無法保存到數據庫中。 請幫我解決這個問題!

更新驗證:

 validates_attachment  :image, 
:content_type => { :content_type => ["image/jpeg", "image/gif", "image/png"] },
:size => { :less_than => 10.megabyte }

查看您從回形針獲得的信息

[paperclip] Content Type Spoof: Filename Capture.JPG (["image/jpeg"]), content type discovered from file command: . See documentation to allow this combination.

您上傳的不是jpeg圖像,而是包含其他格式。 嘗試上傳內容類型與文件擴展名匹配的圖像。

我會看回形針文檔,看看如何處理這種情況

問題出在允許的參數中,它應按以下方式代為指導員,而非駕駛員:

def permitted_params
 params.require(:instructor).permit(..., :avatar)
end 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM