我是个 ruby 新手,正在学习七牛云存储的一些 gem,这个 gem 十分好使,并且在给出的 demo 里面运行良好 就是这个:https://github.com/huobazi/qiniu_direct_uploader 以下是实现表单的代码:
<%= qiniu_uploader_form callback_url: qiniu_callback_path,
bucket: 'mosite',
id: "photograph-uploader",
key: "items/#{Date.today.strftime('%Y/%m')}/{timestamp}-{unique-id}-#{SecureRandom.hex}-{filename}",
custom_fields: {iid:"#{@item.id}",bbb:2},
progress_bar_id: 'progress-bar',
callbackBody: "name=$(fname)&key=$(key)&iid=$(iid)\
&bbb=$(bbb)&iwd=12345",
drop_paste_zone_id: 'dropzone' do %>
当我喜闻乐见的把 demo 中的表单搬到我的 project 里面的时候,我惊奇的发现{timestamp}、{filename}、{unique-id}等变量全挂了
Started POST "/images/qiniu_callback" for 127.0.0.1 at 2015-05-25 22:11:14 +0800
Processing by ImagesController#qiniu_callback as */*
Parameters: {"etag"=>"FmNco5xtUQvPhZOdMiqVBFG8fiJM", "fname"=>"df2.jpeg", "fsize"=>"131663", "mimeType"=>"image/jpeg", "imageInfo"=>{"format"=>"jpeg", "width"=>"853", "height"=>"640", "colorModel"=>"ycbcr"}, "endUser"=>"", "key"=>"items/2015/05/{timestamp}-{unique-id}-3e545506ff0541cbd4d08f06e57a45ef-{filename}", "custom_fields"=>{"iid"=>"105", "bbb"=>"2"}, "xyz"=>"100"}
当然,demo 里面的是没问题的,应该是这样的:
Parameters: {"etag"=>"FmNco5xtUQvPhZOdMiqVBFG8fiJM", "fname"=>"df2.jpeg", "fsize"=>"131663", "mimeType"=>"image/jpeg", "imageInfo"=>{"format"=>"jpeg", "width"=>"853", "height"=>"640", "colorModel"=>"ycbcr"}, "endUser"=>"", "key"=>"items/2015/05/1432563304845-ny1pk2yr4fgupr3exgsfh4-9091677139ee85f92e54ad1dc34208bf-df2.jpeg", "custom_fields"=>{"iid"=>"51", "bbb"=>"2"}, "xyz"=>"100"}
就这样,上传到七牛空间本来应该是有名字的文件都被字符串“filename”取代了,这样就很蛋疼。 那么像{timestamp}、{filename}、{unique-id}这样的变量到底是什么变量?应该不是 ruby 变量,是一个 js 变量么? 于是我去除了多余的 js,仅仅保留了
//= require jquery
//= require jquery_ujs
//= require turbolinks
//
//= require_tree .
这几行,以避免 js 冲突,但是还是显示{filename} = =
我修改了 gem 的版本,将版本锁定到 0.0.4,结果就没有问题了
Parameters: {"etag"=>"FmNco5xtUQvPhZOdMiqVBFG8fiJM", "fname"=>"df2.jpeg", "fsize"=>"131663", "mimeType"=>"image/jpeg", "imageInfo"=>{"format"=>"jpeg", "width"=>"853", "height"=>"640", "colorModel"=>"ycbcr"}, "endUser"=>"", "key"=>"items/2015/05/1432644798226-cypsqzdouy0c5wduf0umkr-5328df4f5c501e33a62de28639af9c06-df2.jpeg", "custom_fields"=>{"iid"=>"108", "bbb"=>"2"}, "xyz"=>"100"}