[英]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.