ruby - Shotgun 上的 Sinatra 和 Ruby 1.9.2 问题

标签 ruby ubuntu sinatra

我有一个简单的 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: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 run' /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/

相关文章:

Ruby:如何检索字符串的一部分?

regex - 生成文件上的 Sed

ubuntu - 在 Virtuoso 开源中安装 ODS 框架时出错

rspec - 将 Sinatra 应用程序的测试数据库与开发数据库分开?

ruby - 如何从/usr/local 卸载 Ruby?

ruby - 这是有效的 ruby​​ 语法吗?

ruby-on-rails - 将 Rails 4 中的 GET 路由限制为仅应用程序

ruby-on-rails - Rails-ShareTribe。 npm 安装错误(ubuntu 16.04)

javascript - 将变量从 Ruby 传递到 JavaScript

ruby - 在 nginx 和 thin 下部署 Sinatra 应用程序的最快技巧(当然是 linux)