【问题标题】:Where do I put Sinatra files intended to be embedded in a Rails3 app?我应该将要嵌入 Rails3 应用程序的 Sinatra 文件放在哪里?
【发布时间】:2010-10-30 01:14:26
【问题描述】:

我想使用新的 Rails3 功能,该功能允许您在 Rails 中嵌入 Sinatra 应用程序。

在我的路线文件中,我有:

# app names changed to protect the (not so) innocent
MyApp::Application.routes.draw do
  match "/service", :to => MyService
  ...
end

在 /lib/my_service.rb 我有

class MyService < Sinatra::Base
  get "/" do
    "Hello World"
  end
end

当我尝试启动 Rails 服务器时,我得到未初始化的常量 MyService (NameError),我怀疑这意味着我的 Sinatra 文件位于错误的目录中。我应该把它放在哪里?

完整的错误文本:

rails/my_app » rails server       
=> Booting WEBrick
=> Rails 3.0.1 application starting in development on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
Exiting
/Users/marshallyount/Projects/clients/method/rails/my_app/config/routes.rb:2:in `block in <top (required)>': uninitialized constant MyService (NameError)
 from /Users/marshallyount/.rvm/gems/ruby-1.9.2-p0@my_app/gems/actionpack-3.0.1/lib/action_dispatch/routing/route_set.rb:233:in `instance_exec'
 from /Users/marshallyount/.rvm/gems/ruby-1.9.2-p0@my_app/gems/actionpack-3.0.1/lib/action_dispatch/routing/route_set.rb:233:in `draw'
 from /Users/marshallyount/Projects/clients/method/rails/my_app/config/routes.rb:1:in `<top (required)>'
 from /Users/marshallyount/.rvm/gems/ruby-1.9.2-p0@my_app/gems/railties-3.0.1/lib/rails/application.rb:127:in `block in reload_routes!'
 from /Users/marshallyount/.rvm/gems/ruby-1.9.2-p0@my_app/gems/railties-3.0.1/lib/rails/application.rb:127:in `each'
 from /Users/marshallyount/.rvm/gems/ruby-1.9.2-p0@my_app/gems/railties-3.0.1/lib/rails/application.rb:127:in `reload_routes!'
 from /Users/marshallyount/.rvm/gems/ruby-1.9.2-p0@my_app/gems/railties-3.0.1/lib/rails/application.rb:120:in `block in routes_reloader'
 from /Users/marshallyount/.rvm/gems/ruby-1.9.2-p0@my_app/gems/activesupport-3.0.1/lib/active_support/file_update_checker.rb:32:in `call'
 from /Users/marshallyount/.rvm/gems/ruby-1.9.2-p0@my_app/gems/activesupport-3.0.1/lib/active_support/file_update_checker.rb:32:in `execute_if_updated'
 from /Users/marshallyount/.rvm/gems/ruby-1.9.2-p0@my_app/gems/actionpack-3.0.1/lib/action_dispatch/railtie.rb:14:in `block (2 levels) in <class:Railtie>'
 from /Users/marshallyount/.rvm/gems/ruby-1.9.2-p0@my_app/gems/activesupport-3.0.1/lib/active_support/callbacks.rb:419:in `_run_prepare_callbacks'
 from /Users/marshallyount/.rvm/gems/ruby-1.9.2-p0@my_app/gems/actionpack-3.0.1/lib/action_dispatch/middleware/callbacks.rb:40:in `initialize'
 from /Users/marshallyount/.rvm/gems/ruby-1.9.2-p0@my_app/gems/actionpack-3.0.1/lib/action_dispatch/middleware/stack.rb:33:in `new'
 from /Users/marshallyount/.rvm/gems/ruby-1.9.2-p0@my_app/gems/actionpack-3.0.1/lib/action_dispatch/middleware/stack.rb:33:in `build'
 from /Users/marshallyount/.rvm/gems/ruby-1.9.2-p0@my_app/gems/actionpack-3.0.1/lib/action_dispatch/middleware/stack.rb:79:in `block in build'
 from /Users/marshallyount/.rvm/gems/ruby-1.9.2-p0@my_app/gems/actionpack-3.0.1/lib/action_dispatch/middleware/stack.rb:79:in `each'
 from /Users/marshallyount/.rvm/gems/ruby-1.9.2-p0@my_app/gems/actionpack-3.0.1/lib/action_dispatch/middleware/stack.rb:79:in `inject'
 from /Users/marshallyount/.rvm/gems/ruby-1.9.2-p0@my_app/gems/actionpack-3.0.1/lib/action_dispatch/middleware/stack.rb:79:in `build'
 from /Users/marshallyount/.rvm/gems/ruby-1.9.2-p0@my_app/gems/railties-3.0.1/lib/rails/application.rb:162:in `app'
 from /Users/marshallyount/.rvm/gems/ruby-1.9.2-p0@my_app/gems/railties-3.0.1/lib/rails/application/finisher.rb:35:in `block in <module:Finisher>'
 from /Users/marshallyount/.rvm/gems/ruby-1.9.2-p0@my_app/gems/railties-3.0.1/lib/rails/initializable.rb:25:in `instance_exec'
 from /Users/marshallyount/.rvm/gems/ruby-1.9.2-p0@my_app/gems/railties-3.0.1/lib/rails/initializable.rb:25:in `run'
 from /Users/marshallyount/.rvm/gems/ruby-1.9.2-p0@my_app/gems/railties-3.0.1/lib/rails/initializable.rb:50:in `block in run_initializers'
 from /Users/marshallyount/.rvm/gems/ruby-1.9.2-p0@my_app/gems/railties-3.0.1/lib/rails/initializable.rb:49:in `each'
 from /Users/marshallyount/.rvm/gems/ruby-1.9.2-p0@my_app/gems/railties-3.0.1/lib/rails/initializable.rb:49:in `run_initializers'
 from /Users/marshallyount/.rvm/gems/ruby-1.9.2-p0@my_app/gems/railties-3.0.1/lib/rails/application.rb:134:in `initialize!'
 from /Users/marshallyount/.rvm/gems/ruby-1.9.2-p0@my_app/gems/railties-3.0.1/lib/rails/application.rb:77:in `method_missing'
 from /Users/marshallyount/Projects/clients/method/rails/my_app/config/environment.rb:5:in `<top (required)>'
 from /Users/marshallyount/Projects/clients/method/rails/my_app/config.ru:3:in `require'
 from /Users/marshallyount/Projects/clients/method/rails/my_app/config.ru:3:in `block in <main>'
 from /Users/marshallyount/.rvm/gems/ruby-1.9.2-p0@my_app/gems/rack-1.2.1/lib/rack/builder.rb:46:in `instance_eval'
 from /Users/marshallyount/.rvm/gems/ruby-1.9.2-p0@my_app/gems/rack-1.2.1/lib/rack/builder.rb:46:in `initialize'
 from /Users/marshallyount/Projects/clients/method/rails/my_app/config.ru:1:in `new'
 from /Users/marshallyount/Projects/clients/method/rails/my_app/config.ru:1:in `<main>'
 from /Users/marshallyount/.rvm/gems/ruby-1.9.2-p0@my_app/gems/rack-1.2.1/lib/rack/builder.rb:35:in `eval'
 from /Users/marshallyount/.rvm/gems/ruby-1.9.2-p0@my_app/gems/rack-1.2.1/lib/rack/builder.rb:35:in `parse_file'
 from /Users/marshallyount/.rvm/gems/ruby-1.9.2-p0@my_app/gems/rack-1.2.1/lib/rack/server.rb:162:in `app'
 from /Users/marshallyount/.rvm/gems/ruby-1.9.2-p0@my_app/gems/rack-1.2.1/lib/rack/server.rb:248:in `wrapped_app'
 from /Users/marshallyount/.rvm/gems/ruby-1.9.2-p0@my_app/gems/rack-1.2.1/lib/rack/server.rb:213:in `start'
 from /Users/marshallyount/.rvm/gems/ruby-1.9.2-p0@my_app/gems/railties-3.0.1/lib/rails/commands/server.rb:65:in `start'
 from /Users/marshallyount/.rvm/gems/ruby-1.9.2-p0@my_app/gems/railties-3.0.1/lib/rails/commands.rb:30:in `block in <top (required)>'
 from /Users/marshallyount/.rvm/gems/ruby-1.9.2-p0@my_app/gems/railties-3.0.1/lib/rails/commands.rb:27:in `tap'
 from /Users/marshallyount/.rvm/gems/ruby-1.9.2-p0@my_app/gems/railties-3.0.1/lib/rails/commands.rb:27:in `<top (required)>'
 from script/rails:6:in `require'
 from script/rails:6:in `<main>'

【问题讨论】:

    标签: ruby-on-rails-3 sinatra


    【解决方案1】:

    感谢this article,我找到了这个问题的好答案。

    我的路由文件顶部缺少所有重要的 requires 标头:

    require 'my_service.rb'
    
    # app names changed to protect the (not so) innocent
    MyApp::Application.routes.draw do
      match "/service", :to => MyService
      ...
    end
    

    【讨论】:

      猜你喜欢
      • 2010-09-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-24
      • 2014-01-14
      • 2010-10-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多