undefined method `run' for main:Object (NoMethodError) Sinatra

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-04 22:04:23

问题


require 'sinatra/base'

class Foo < Sinatra::Base
  get('/foo') { 'foo' }
end

class Bar < Sinatra::Base
  get('/bar') { 'bar' }
end

run Rack::Cascade, [Foo, Bar]

I just can't guess what is wrong with this code. When I ran: ruby server.rb, it throws an error


回答1:


First of all, the last line should read

run Rack::Cascade.new [Foo, Bar]

But you can only use this in a Rackup File. So second, you need to create a File called config.ru (Rackup File) with the following contents:

require './app'
run Rack::Cascade.new [Foo, Bar]

and a file called app.rb with your actual app:

require 'sinatra/base'

class Foo < Sinatra::Base
  get('/foo') { 'foo' }
end

class Bar < Sinatra::Base
  get('/bar') { 'bar' }
end

then you can start the server by typing in the command line

$ rackup
>> Thin web server (v1.3.1 codename Triple Espresso)
>> Maximum connections set to 1024
>> Listening on 0.0.0.0:9292, CTRL+C to stop

after that, open a second command line window and test your app:

$ curl 0.0.0.0:9292/foo
foo%
$ curl 0.0.0.0:9292/bar
bar%    


来源:https://stackoverflow.com/questions/9786540/undefined-method-run-for-mainobject-nomethoderror-sinatra

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!