Use Paperclip in rack app like sinatra and how to write test

Tips,ruby 8 十一月 2011 | View Comments


You’ll find rack parse uploaded file in different format to Rails

Here is the format in rack app.

"image"=>{:type=>"image/jpeg", :filename=>"listing[image]", :tempfile=>#<File:/tmp/RackMultipart20111107-16008-erra0z-0>, :head=>"Content-Disposition: form-data; name=\"listing[image]\"; filename=\"listing[image]\"\r\nContent-Type: image/jpeg\r\n", :name=>"listing[image]"}

You can simply covert it to right format.

post "some_api" do
yourModel = YourModel.new(:image => to_paperclip(params['image']))
yourModel.save
end
def to_paperclip(image)
  paperclip = {}
  paperclip['tempfile'] = image[:tempfile]
  paperclip['filename'] = image[:filename]
  paperclip['content_type'] = image[:type]
  paperclip['size'] = image[:tempfile].size
  paperclip
end

How to test

 filename = Rails.root.join "spec/fixtures/rails.png"
 file = Rack::Test::UploadedFile.new(filename, "image/png")
 post "/api/v1/some_api", {:image => file}
 # should have image uploaded



Tagged in , , ,

  • http://goo.gl/Wdmf0 陈震南

    每天结算,试试看吧。

  • http://goo.gl/Hh8tW 爱分享

    多位站长的实践数据显示,我们平台的用户转化率相当高,每1000IP平均可以为你带来50~60元收入,欢迎加入。你可以通过网站、发帖、微博、QQ等任何方式进行推广,只要你能带来IP,收益就一定有保证。

blog comments powered by Disqus

Ads Plugin created by Jake Ruston's Wordpress Plugins - Powered by and football database.