2014-03-31 58 views
0

當我試圖執行以下代碼`encode_www_form ':未定義的方法`地圖'

require "net/http" 
require "uri" 

@appArgs = Array.new 
@vmip_and_port = "yyyyy" 

def post(url, data) 
    uri = URI.parse(url) 
    http = Net::HTTP.new(uri.host, uri.port) 
    request = Net::HTTP::Post.new(uri.request_uri) 
    request.set_form_data(data) 
    response = http.request(request) 
    return response 
end 

def click_inapp(id) 
    @appArgs.push(id) 
    return 
end 

def finish_inapp() 
    data = @appArgs.join(",") 
    res = post("http://" [email protected]_and_port+"/", data) 
    return res.body 
end 
click_inapp("5X7_photo_paper.png") 
click_inapp("4X6_photo_paper.png") 
finish_inapp() 

正在逐漸

uri/common.rb:931:in `encode_www_form': undefined method `map' for "5X7_photo_paper.png,4X6_photo_paper.png":String (NoMethodError) 
    from /home/ushusadmin/.rvm/rubies/ruby-1.9.3-p448/lib/ruby/1.9.1/net/http.rb:1761:in `set_form_data' 
    from app_utilities.rb:11:in `post' 
    from app_utilities.rb:23:in `finish_inapp' 
    from app_utilities.rb:28:in `<main>' 

什麼毛病我code.please corect我

回答

3

set_form_data被期望Hash的字段名稱和值,而不是string。因此,嘗試:

require "net/http" 
require "uri" 

@appArgs = Hash.new 
@vmip_and_port = "172.16.2.40:9090" 

def post(url, data) 
    uri = URI.parse(url) 
    http = Net::HTTP.new(uri.host, uri.port) 
    request = Net::HTTP::Post.new(uri.request_uri) 
    request.set_form_data(data) 
    response = http.request(request) 
    return response 
end 

def click_inapp(field_name, id) 
    @appArgs[field_name] = id 
end 

def finish_inapp() 
    res = post("http://" [email protected]_and_port+"/", @appArgs) 
    return res.body 
end 
click_inapp("png1", "5X7_photo_paper.png") 
click_inapp("png2", "4X6_photo_paper.png") 
finish_inapp() 

我真的不知道有關該計劃的其餘部分,因爲我不想要測試,並在172.16.2.40:9090 :-)

攻擊你的服務器的控制檯上嘗試ri Net::HTTP ,舉例說明。

+0

:-) thanks yooooooo – Sush