2012-01-14 40 views
0

這基本上就是我想要做的,用表單中給出的參數,我想對一個網站做一個GET/POST請求,這個網站期望一個特定的像http://site.com/user=XXX&size=XXX這樣的URL,它會讓我回到JSON,當表單提交時,我想從這個JSON解析/保存數據到我的Rails應用程序中。Rails form => URL => JSON =>保存參數

我完全失去了這種方式,任何東西都將非常感激。

Rails的表單數據=>生成的URL =>做一個GET/POST請求=>抓住JSON =>解析=>保存

回答

0

我猜你想要做的另一個不是你的站點的請求,以獲得響應。所以你可以安裝遏制寶石(紅寶石捲曲包裝)。然後用它在另一個站點上發出請求,並使用像Json這樣的標準RoR工具來解析json以進行散列。

0

http://www.rubyinside.com/nethttp-cheat-sheet-2940.html你得到你可以做到以下幾點:

在您的文件頂部添加:

require "net/http" 
require "uri" 
require 'json' 

然後在您的控制器中或幫手:

#set the uri 
uri = URI.parse("http://my.site.com/uri") 

#set the post params and get the respons 
response = Net::HTTP.post_form(uri, {"first_param" => "my param", "second_param" => "another param"}) 

#get the json info 
data = JSON.parse(response.body) 

#set result to an ActiveRecord (maybe there is a better way to do this, I guess it depends on the response you get 
@something = Mymodel.new 
@something.name = data["name"] 
... 
@something.save 

希望它有幫助!