我有一个简单的 sinatra 应用。
require 'rubygems'
require 'sinatra'
get '/' do
"Hello"
end
当我在 Shotgun 上运行它时,出现以下错误:
Boot Error
Something went wrong while loading simple.rb
LoadError: no such file to load -- simple.rb
:29:in
require' <internal:lib/rubygems/custom_require>:29:in
require' /home/thedinga/.rvm/gems/ruby-1.9.2-p0@global/gems/shotgun-0.8/lib/shotgun/loader.rb:114:ininner_app' /home/thedinga/.rvm/gems/ruby-1.9.2-p0@global/gems/shotgun-0.8/lib/shotgun/loader.rb:102:in
assemble_app' /home/thedinga/.rvm/gems/ruby-1.9.2-p0@global/gems/shotgun-0.8/lib/shotgun/loader.rb:86:inproceed_as_child' /home/thedinga/.rvm/gems/ruby-1.9.2-p0@global/gems/shotgun-0.8/lib/shotgun/loader.rb:31:in
call!' /home/thedinga/.rvm/gems/ruby-1.9.2-p0@global/gems/shotgun-0.8/lib/shotgun/loader.rb:18:incall' /home/thedinga/.rvm/gems/ruby-1.9.2-p0@global/gems/shotgun-0.8/lib/shotgun/favicon.rb:12:in
call' /home/thedinga/.rvm/gems/ruby-1.9.2-p0@global/gems/rack-1.2.1/lib/rack/builder.rb:77:incall' /home/thedinga/.rvm/gems/ruby-1.9.2-p0@global/gems/rack-1.2.1/lib/rack/content_length.rb:13:in
call' /home/thedinga/.rvm/gems/ruby-1.9.2-p0@global/gems/rack-1.2.1/lib/rack/handler/webrick.rb:52:inservice' /home/thedinga/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1/webrick/httpserver.rb:111:in
service' /home/thedinga/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1/webrick/httpserver.rb:70:inrun' /home/thedinga/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1/webrick/server.rb:183:in
block in start_thread'
如果我使用 ruby simple.rb
我没有使用 shotgun,而是在浏览器中得到了您期望的输出。作为旁注,如果我将它推送到 Heroku(我真的很想在其上运行 sinatra 应用程序),Heroku 也将无法运行该应用程序。这是 1.9.2 的版本问题吗?还是我错过了其他东西?
最佳答案
上面的代码有效,只需将 config.ru 文件中的 require '#{path}/myapp' 更正为 require "#{path}/myapp"即可。
在 ruby 中,只有带有 "字符的字符串可以使用内部 #{}。在带有 ' 字符的字符串中,字符串将继续为 '#{path}/myapp' 而不是 'value/of/path/variabel/myapp'。
所以可以像下面那样做
# FILE config.ru
path = File.expand_path "../", __FILE__
require 'rubygems'
require 'sinatra'
require "#{path}/myapp"
run Sinatra::Application
# FILE myapp.rb
get '/' do
'hello'
end
在应用程序根目录中启动刚刚运行命令 shotgun 的应用程序
关于ruby - Shotgun 上的 Sinatra 和 Ruby 1.9.2 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4346981/