使用回形针,红宝石在轨道上无法保存图像

时间:2014-12-19 10:29:01

标签: ruby-on-rails ruby paperclip

我使用回形针上传图片。我的应用可以正确导入图像,但我无法保存。这是我的控制器:

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 }

2 个答案:

答案 0 :(得分:0)

查看从Paperclip获得的信息

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

您上传的内容不是jpeg图片,但它包含不同的格式。尝试上传内容类型与文件扩展名匹配的图片。

我会查看回形针文档以了解如何处理这种情况

答案 1 :(得分:0)

问题出现在允许的参数中,它应该是指导者而不是驾驶员,如下所示:

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