簡體   English   中英

無法使用回形針保存圖像

[英]Can't save image with paperclip

我正在使用回形針拖放帶有dropzone的圖像。 而且我在將圖像插入數據庫時​​遇到問題。 因此,請幫助我解決該問題。 當我檢查日志文件時,這是我的錯誤:

Processing by UploadsController#create as JSON
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"iuwYSudAJbAJMuJ2CLLdNhHF1o3pS1uxfwE+T7Li1mw=", "upload"=>{"image"=>#<ActionDispatch::Http::UploadedFile:0x3350fe8 @original_filename="Koala.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"upload[image]\"; filename=\"Koala.jpg\"\r\nContent-Type: image/jpeg\r\n", @tempfile=#<File:C:/Users/QUANGD~1/AppData/Local/Temp/RackMultipart20141110-4904-1ecpi5y>>}}
  [1m[35m (1.0ms)[0m  begin transaction
  [1m[36m (0.0ms)[0m  [1mrollback transaction[0m
image: /images/original/missing.png
  [1m[35m (0.0ms)[0m  begin transaction
  [1m[36m (0.0ms)[0m  [1mrollback transaction[0m
Completed 400 Bad Request in 28.0ms (Views: 0.0ms | ActiveRecord: 1.0ms)

我不知道失蹤是什么。 在此處輸入圖片說明

這是我的上傳模型

class Upload < ActiveRecord::Base
  attr_accessible :image
  has_attached_file :image, :styles => { :medium => "300x300>",:thumb => "100x100>" }

  validates_attachment  :image, 
        :presence => true,
        :content_type => { :content_type => /\Aimage\/.*\Z/ },
        :size => { :less_than => 1.megabyte }
end

我的上傳控制器

class UploadsController < ApplicationController
  def new
    @upload = Upload.new
  end

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

我上傳的HTML

<script>
    $(document).ready(function(){
    // disable auto discover
    Dropzone.autoDiscover = false;

    // grap our upload form by its id
    $("#new_upload").dropzone({
        // restrict image size to a maximum 1MB
        maxFilesize: 1,
        // changed the passed param to one accepted by
        // our rails app
        paramName: "upload[image]",
        // show remove links on each image upload
        addRemoveLinks: true
    }); 
});
</script>
<div class="medium-4 medium-centered row">
    <div class="medium-10 medium-centered columns">
        <%= form_for(@upload, html: { multipart: true, class: "dropzone"}) do |f|  %>
        <div class="fallback">
            <%= f.file_field :image %>
            <br>
            <%= f.submit "Upload" %>
        </div>
        <% end %>
    </div>
</div>

查看您的日志:

Parameters: {"upload"=>{"image"=>#<ActionDispatch::Http::UploadedFile:0x3350fe8 @original_filename="Koala.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"upload[image]\"; filename=\"Koala.jpg\"\r\nContent-Type: image/jpeg\r\n", @tempfile=#<File:C:/Users/QUANGD~1/AppData/Local/Temp/RackMultipart20141110-4904-1ecpi5y>>}}

並在html腳本中:

# .....   
paramName: "upload[image]"
# ..... 

所以我認為您應該修復create動作:

# ..... 
@upload = Upload.create(:image => params[:upload][:image])
# ..... 

暫無
暫無

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

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