根据我在某人博客上找到的示例,我使用以下代码将我的 acme 应用程序中的 Rails 模型嵌入(或者至少我认为我已经嵌入)到我的 Sinatra 应用程序中。rails 应用程序在/Users/chris/acme
,Sinatra 应用程序在/Users/chris/acme/services/sinatra/sinatra.rb
.
RAILS_ROOT = '../..'
LIB_DIR = "#{RAILS_ROOT}/lib"
MODELS_DIR = "#{RAILS_ROOT}/app/models"
require 'active_support/dependencies'
ActiveSupport::Dependencies.autoload_paths += Dir["#{LIB_DIR}/**/"]
ActiveSupport::Dependencies.autoload_paths += Dir["#{MODELS_DIR}/"]
require 'active_support/all'
require 'active_record'
::ActiveRecord::Base.establish_connection(
YAML.load(File.read(
File.expand_path('config/database.yml', RAILS_ROOT)))["development"])
require 'sinatra'
get '/user' do
"#{User.first.id}"
end
当我访问时,http://localhost:4567/user
我收到以下错误:
LoadError - no such file to load -- util/versioning:
/Users/chris/.rvm/gems/ruby-1.9.2-p320@acme/gems/activesupport-3.0.20/lib/active_support/dependencies.rb:242:in `require'
/Users/chris/.rvm/gems/ruby-1.9.2-p320@acme/gems/activesupport-3.0.20/lib/active_support/dependencies.rb:242:in `block in require'
/Users/chris/.rvm/gems/ruby-1.9.2-p320@acme/gems/activesupport-3.0.20/lib/active_support/dependencies.rb:225:in `block in load_dependency'
/Users/chris/.rvm/gems/ruby-1.9.2-p320@acme/gems/activesupport-3.0.20/lib/active_support/dependencies.rb:597:in `new_constants_in'
/Users/chris/.rvm/gems/ruby-1.9.2-p320@acme/gems/activesupport-3.0.20/lib/active_support/dependencies.rb:225:in `load_dependency'
/Users/chris/.rvm/gems/ruby-1.9.2-p320@acme/gems/activesupport-3.0.20/lib/active_support/dependencies.rb:242:in `require'
/Users/chris/acme/app/models/user.rb:2:in `<top (required)>'
第 2 行user.rb
is require 'util/versioning'
,这应该是自动加载的,但没有。
如果我将第 2 行更改为user.rb
,require '/Users/chris/acme/lib/util/versioning'
那么一切都会开始工作,但必须有更好的方法,对吧?