我正在尝试将params的哈希传递给sinatra中的post post资源中的函数,请参阅下文。
post '/service' do
create_itop_entry(params)
end
我的功能是我将params传递给....
def create_itop_entry(fields)
params = {'json_data' => '{"operation":"core/create",
"comment":"Service created on ' + Time.now.strftime("%Y-%m-%d") + '",
"class":"Service",
"fields":{
org_id: "'+ fields["org_id"] +'",
status: "' + fields["status"] + '",
stack_type: "' + fields["stack_type"] + '",
version: "' + fields["version"] + '",
hostname: "' + fields["hostname"] + '",
longitude: "' + fields["longitude"] + '",
latitude: "' + fields["latitude"] + '",
abbreviation_code: "' + fields["abbreviation_code"] + '",
name: "' + fields["name"] + '",
email: "' + fields["email"] + '",
apn: "' + fields["apn"] + '"
},
"output_fields":"id, friendlyname"
}'
}
end
下面的函数调用返回我的预期结果...这是添加到我的json_data的哈希值。当我从sinatra打印出params.inspect时,它会返回与下面相同的哈希值。
puts create_itop_entry({"org_id"=>"4", "status"=>"pilot", "stack_type"=>"single", "version"=>"2.9.29", "hostname"=>"test.pilot.net", "longitude"=>"-81.20", "latitude"=>"41.20", "abbreviation_code"=>"zzz", "name"=>"Test-Test", "email"=>"test@testing.com", "apn"=>"TestRange1 10.36.192.80-28,TestRange2 10.131.201.0-24"})
我得到的错误是没有将Array隐式转换为String ...我是ruby / sinatra的新手,所以我可能会错过一些简单的...
[2016-08-27 17:28:03] DEBUG WEBrick::HTTPServlet::FileHandler is mounted on /.
[2016-08-27 17:28:03] DEBUG Rack::Handler::WEBrick is mounted on /.
[2016-08-27 17:28:03] INFO WEBrick::HTTPServer#start: pid=32343 port=4567
[2016-08-27 17:28:14] DEBUG accept: ::1:58203
[2016-08-27 17:28:14] DEBUG Rack::Handler::WEBrick is invoked.
[2016-08-27 17:28:15] ERROR TypeError: no implicit conversion of Array into String
/home/geapp/.gem/ruby/2.0/gems/rack-1.6.4/lib/rack/handler/webrick.rb:113:in `block in service'
/home/geapp/.gem/ruby/2.0/gems/rack-1.6.4/lib/rack/handler/webrick.rb:112:in `each'
/home/geapp/.gem/ruby/2.0/gems/rack-1.6.4/lib/rack/handler/webrick.rb:112:in `service'
/usr/share/ruby/2.0/webrick/httpserver.rb:138:in `service'
/usr/share/ruby/2.0/webrick/httpserver.rb:94:in `run'
/usr/share/ruby/2.0/webrick/server.rb:295:in `block in start_thread'
localhost - - [27/Aug/2016:17:28:14 UTC] "POST /service HTTP/1.1" 500 338
答案 0 :(得分:4)
您的函数返回一个哈希值。这不是return from a Sinatra route的有效类型。返回哈希导致Web服务器混淆,导致此错误。
也许您的意思是调用to_json
并返回结果字符串?