Sinatra未能回答非常基本的GET请求

时间:2012-01-13 20:33:58

标签: ruby sinatra ruby-1.9

我有以下Gemfile:

source "http://rubygems.org"

gem 'sinatra', '1.3.2'
gem 'json', '1.6.4'

以下Sinatra应用程序:

require 'sinatra'
require 'json'

get '/ze/api/session.json' do
  content_type :json
  { :name => 'name' }
end

当我提出像这样的基本要求时:

curl localhost:4567/ze/api/session.json

我明白了:

[2012-01-13 17:30:36] ERROR TypeError: can't convert Array into String
/Users/mauricio/.rvm/gems/ruby-1.9.2-p290@office-drop-sync/gems/rack-1.4.0/lib/rack/handler/webrick.rb:72:in `block in service'
/Users/mauricio/.rvm/gems/ruby-1.9.2-p290@office-drop-sync/gems/rack-1.4.0/lib/rack/handler/webrick.rb:71:in `each'
/Users/mauricio/.rvm/gems/ruby-1.9.2-p290@office-drop-sync/gems/rack-1.4.0/lib/rack/handler/webrick.rb:71:in `service'
/Users/mauricio/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/webrick/httpserver.rb:111:in `service'
/Users/mauricio/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/webrick/httpserver.rb:70:in `run'
/Users/mauricio/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/webrick/server.rb:183:in `block in start_thread'
localhost - - [13/Jan/2012:17:30:36 BRT] "GET /ze/api/session.json HTTP/1.1" 500 311

我在Lion Mac中使用Ruby 1.9.2和RVM。

1 个答案:

答案 0 :(得分:4)

您尝试从路由返回哈希值,但哈希值不是something you can return here

只需使用.to_json将哈希值转换为json字符串,然后您可以返回(您需要json,但尚未使用它):

get '/ze/api/session.json' do
  content_type :json
  { :name => 'name' }.to_json
end