Sinatra 无法响应非常基本的 GET 请求
我有以下 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 中使用带有 RVM 的 Ruby 1.9.2。
I have the following Gemfile:
source "http://rubygems.org"
gem 'sinatra', '1.3.2'
gem 'json', '1.6.4'
And the following Sinatra application:
require 'sinatra'
require 'json'
get '/ze/api/session.json' do
content_type :json
{ :name => 'name' }
end
And when I make a basic request like this one:
curl localhost:4567/ze/api/session.json
I get this:
[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
I am using Ruby 1.9.2 with RVM in a Lion Mac.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您尝试从路由返回哈希值,但哈希值不是 您可以的东西返回此处。
只需使用
.to_json
将哈希值转换为 json 字符串,然后您可以返回该字符串(您需要 json,但尚未使用它):You're trying to return a hash from the route, but a hash isn't something you can return here.
Simply use
.to_json
to turn the hash into a json string which you can then return (you've required json, but aren't using it yet):