-1
我正在开发一个rails api only app。我已经集成了剪辑,但是通过POSTMAN和CURL保存后,我都获得了成功响应,记录得到创建,但只有created_at & updated_at字段得到保存,其他所有内容仍然为零。 如果我在控制台上创建记录一切正常。 型号回形针没有保存图片,只有creadted_at和updated_at被存储,其他所有东西都是零?
class Picture < ActiveRecord::Base
attr_accessor :image, :image_attributes
has_attached_file :image, :styles => {:medium =>"300x300"}
validates_attachment_content_type :image, :content_type => /\Aimage\/.*\Z/
end
控制器
class PicturesController < ApplicationController
def new
picture = Picture.new
end
def create
print 'dir is'
puts Dir.pwd
result = { status: "failed" }
begin
picture = Picture.new
if picture.valid? && picture.save
result[:status] = "success"
end
rescue Exception => e
Rails.logger.error "#{e.message}"
end
render json: result.to_json
ensure
clean_tempfile
end
def picture_params
params.require(:picture).permit(:title,:image)
end
def parse_image_data(image_data)
@tempfile = Tempfile.new('item_image')
@tempfile.binmode
@tempfile.write Base64.decode64(image_data[:content])
@tempfile.rewind
uploaded_file = ActionDispatch::Http::UploadedFile.new(
tempfile: @tempfile,
filename: image_data[:filename]
)
uploaded_file.content_type = image_data[:content_type]
uploaded_file
end
def clean_tempfile
if @tempfile
@tempfile.close
@tempfile.unlink
end
end
end
我的服务器日志的跟踪。
Started POST "/pictures?title=new" for ::1 at 2016-01-13 22:26:04 +0530
Processing by PicturesController#create as */*
Parameters: {"image"=>#<ActionDispatch::Http::UploadedFile:0x00000004924b50 @tempfile=#<Tempfile:/tmp/RackMultipart20160113-3226-brqash.png>, @original_filename="philosphy.png", @content_type="image/png", @headers="Content-Disposition: form-data; name=\"image\"; filename=\"philosphy.png\"\r\nContent-Type: image/png\r\n">, "title"=>"new"}
dir is/home/anjan/Development/frankly_speak
(0.2ms) BEGIN
SQL (0.2ms) INSERT INTO `pictures` (`created_at`, `updated_at`) VALUES ('2016-01-13 16:56:04', '2016-01-13 16:56:04')
(38.2ms) COMMIT
Completed 200 OK in 75ms (Views: 0.1ms | ActiveRecord: 40.0ms)
哪里是调用'#parse_image_data'? –
@МалъСкрылевъ嗨,不知道在哪里叫它,你能指导我吗? – Anjan
我不知道回形针应该继续上传的图像本身。如何做它请参考回形针手册 –