Rails 3.1可安装引擎gem依赖项

时间:2012-01-25 11:03:50

标签: ruby-on-rails rails-engines rails-3.1

我正在为我的应用程序使用api。 api是一种可安装的引擎。

我的问题是,如果未在父应用程序gemfile中指定gem,则它们不会加载到api引擎中。

我目前的设置:

parentapp / Gemfile中

gem 'api', :path => 'path/to/api'

parentapp / routes.rb中

mount Api::Engine => '/api'

api.gemspec

$:.push File.expand_path("../lib", __FILE__)

require "api/version"

Gem::Specification.new do |s|
  s.name        = "api"
  s.version     = Api::VERSION
  s.authors     = ["Author"]
  s.email       = ["email"]
  s.homepage    = "http://example.com"
  s.summary     = "Summary"
  s.description = "Description"

  s.files = Dir["{app,config,db,lib}/**/*"] + ["MIT-LICENSE", "Rakefile", "README.rdoc"]
  s.test_files = Dir["test/**/*"]

  # Is not loading.
  s.add_dependency 'rails', '~> 3.1.3'
  s.add_dependency 'rabl'
  s.add_dependency 'yajl-ruby'
end

API /测试/虚设/配置/ routes.rb中

Rails.application.routes.draw do

  mount Api::Engine => "/api"
end

奇怪的是它正在“加载”,因为如果我设置

s.add_dependency 'rails', '~> 3.2'

它引发了一个错误。

如果我从父应用程序gemfile中删除gem 'rabl',则为Stacktrace。

NameError: uninitialized constant Rabl
~/Sites/api/config/initializers/rabl_init.rb:1:in `<top (required)>'
~/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/railties-3.1.3/lib/rails/engine.rb:556:in `block (2 levels) in <class:Engine>'
    ~/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/railties-3.1.3/lib/rails/engine.rb:555:in `each'
    ~/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/railties-3.1.3/lib/rails/engine.rb:555:in `block in <class:Engine>'
    ~/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/railties-3.1.3/lib/rails/initializable.rb:30:in `instance_exec'
    ~/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/railties-3.1.3/lib/rails/initializable.rb:30:in `run'
    ~/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/railties-3.1.3/lib/rails/initializable.rb:55:in `block in run_initializers'
    ~/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/railties-3.1.3/lib/rails/initializable.rb:54:in `each'
    ~/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/railties-3.1.3/lib/rails/initializable.rb:54:in `run_initializers'
    ~/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/railties-3.1.3/lib/rails/application.rb:96:in `initialize!'
    ~/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/railties-3.1.3/lib/rails/railtie/configurable.rb:30:in `method_missing'
    ~/Sites/parentapp/config/environment.rb:5:in `<top (required)>'
    <internal:lib/rubygems/custom_require>:29:in `require'
    <internal:lib/rubygems/custom_require>:29:in `require'
    ~/Sites/parentapp/config.ru:3:in `block in <main>'
    ~/Library/Application Support/Pow/Versions/0.3.2/node_modules/nack/lib/nack/builder.rb:4:in `instance_eval'
    ~/Library/Application Support/Pow/Versions/0.3.2/node_modules/nack/lib/nack/builder.rb:4:in `initialize'
    ~/Sites/parentapp/config.ru:1:in `new'
    ~/Sites/parentapp/config.ru:1:in `<main>'
    ~/Library/Application Support/Pow/Versions/0.3.2/node_modules/nack/lib/nack/server.rb:50:in `eval'
    ~/Library/Application Support/Pow/Versions/0.3.2/node_modules/nack/lib/nack/server.rb:50:in `load_config'
    ~/Library/Application Support/Pow/Versions/0.3.2/node_modules/nack/lib/nack/server.rb:43:in `initialize'
    ~/Library/Application Support/Pow/Versions/0.3.2/node_modules/nack/lib/nack/server.rb:13:in `new'
    ~/Library/Application Support/Pow/Versions/0.3.2/node_modules/nack/lib/nack/server.rb:13:in `run'
    ~/Library/Application Support/Pow/Versions/0.3.2/node_modules/nack/bin/nack_worker:4:in `<main>'

这是我的rabl_init.rb文件,位于config / initializers(在引擎中)

Rabl.configure do |config|
 config.include_json_root = false
end

如果我删除此文件,我会获得Template is missing

Missing template api/v1/myresource/show, api/application/show with {:handlers=>[:erb, :builder, :coffee, :haml], :formats=>[:json]

不包括rabl处理程序,ergo,rabl没有加载。

1 个答案:

答案 0 :(得分:3)

我并没有像我应该那样需要宝石。 在require "rabl"中添加lib/api/engine.rb解决了这个问题。