2014-12-19 81 views
1

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

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 } 

回答

0

縱觀信息從回形針

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

你上傳的不是一個JPEG圖像獲取,但它包含不同的格式。嘗試上傳內容類型與文件擴展名匹配的圖像。

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

+0

我試着導入窗口的示例圖片,但仍然是相同的錯誤。我添加了我的驗證,可能是問題 – 2014-12-19 10:48:33

0

的問題是在允許的參數,可以也應該是指教練,而不是司機按如下:

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

對不起!我沒有使用帶有強參數的rails 4 – 2014-12-19 10:49:30