如何使用RoR将照片上传到flickr?

时间:2011-11-10 16:15:25

标签: ruby-on-rails forms file-upload flickr

我在测试应用中使用Flickraw

关注example

我可以通过Keying文件路径上传文件,但我想使用form_tag helper

这是我的观点

= form_tag :upload_photo_to_Flickr , :multipart => true do 
= hidden_field_tag :token, @upload_info[:token] 
= label_tag "PHOTO" 
= file_field_tag :photo 
= submit_tag "Upload Photo" 

控制器

 def upload_photo_to_Flickr
    file_path = params[:photo]
    flickr.upload_photo file_path , :title => "please" , :description=>"successfully"
 end 

显示错误消息 ArgumentError(错误的参数数量(1表示0))

我还尝试使用read,

的其他方法
 def upload_photo_to_Flickr
    file_path = params[:photo].read
    flickr.upload_photo file_path , :title => "please" , :description=>"successfully"
 end 

它显示错误消息 ArgumentError(字符串包含空字节)

参数是

{"photo"=>#<ActionDispatch::Http::UploadedFile:0x10ba37648 
  @original_filename="test.jpg",
  @tempfile=#<File:/var/folders/wy/5yx_py3s02g8l0kw0frrpg_m0000gn/T/RackMultipart20111110-   
   1832-18ufrmm-0>,
  @headers="Content-Disposition: form-data; name=\"photo\"; 
  filename=\"test.jpg\"\r\nContent-Type: image/jpeg\r\n",
  @content_type="image/jpeg">,

   "commit"=>"Upload Photo \350\201\236",
   "token"=>"XXXX",
   "authenticity_token"=>"o9PhVeE6u1MGfol/PvGQkLE8RCL5tJat+1AQlygGOKc=",
   "utf8"=>"\342\234\223"
}

如何获取文件路径?

因为它可以像这样工作

def upload_photo_to_Flickr
  file_path = "/Desktop/test.jpg"
  flickr.upload_photo file_path , :title => "please" , :description=>"successfully"
end 

或者我必须尝试其他方法来获取文件路径而不是form_tag?

1 个答案:

答案 0 :(得分:2)

我找到了解决方案!

在控制器中,使用

  

flickr.upload_photo(PARAMS [:相片] .tempfile.path)