关于Shotgun的Sinatra和Ruby 1.9.2的问题

Jus*_*liz 5 ruby ubuntu sinatra

我有一个简单的sinatra应用程序.

require 'rubygems'
require 'sinatra'

get '/' do
  "Hello"
end
Run Code Online (Sandbox Code Playgroud)

当我在Shotgun上运行它时,我收到以下错误:

引导错误

加载simple.rb时出错了

LoadError:没有要加载的文件 - 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 :in inner_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:in proceed_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:in call' /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:in call' /home/thedinga/.rvm/gems/ruby-1.9.2-p0@global/gems/rack-1.2.1/lib/rack/content_length.rb:13:incall'/home/thedinga/.rvm/gems/ruby-1.9.2-p0@global/ gems/rack-1.2.1/lib/rack/handler/webrick.rb:52:in service' /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:in start_thread中的 run' /home/thedinga/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1/webrick/server.rb:183:in 块'

如果我使用ruby simple.rb而不是使用猎枪,我会在浏览器中获得您期望的输出.作为旁注,如果我将它推送到Heroku(我真的想要运行sinatra应用程序),Heroku也将无法运行应用程序.这是1.9.2的版本问题吗?还是我错过了别的什么?

小智 8

上面的代码工作,只需要在config.ru文件中要求'#{path}/myapp'来要求"#{path}/myapp".

在ruby中只有带有"caracter的字符串可以使用内部#{}.在带有'caracter的字符串中,字符串将继续为'#{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
Run Code Online (Sandbox Code Playgroud)

应用程序在应用程序根目录下运行命令shotgun