2

我试图在 Rails 3.2.11 应用程序中运行 Grape API。根据葡萄文档,我应该在我的 config/application.rb 中添加以下几行。

# Auto-load API and its subdirectories
config.paths.add "app/api", :glob => "**/*.rb"
config.autoload_paths += Dir["#{Rails.root}/app/api/*"]

并使用以下代码创建 config/initializers/reload_api.rb:

if Rails.env.development?
  api_files = Dir["#{Rails.root}/app/api/**/*.rb"]
  api_reloader = ActiveSupport::FileUpdateChecker.new(api_files) do
    Rails.application.reload_routes!
  end
  ActionDispatch::Callbacks.to_prepare do
    api_reloader.execute_if_updated
  end
end

但这对我不起作用。这是我的代码。在我的 app/api/v1.rb 中:

module Api
  class V1 < Grape::API

    get '/hello' do
      []
    end
  end
end

还有我的 config/routes.rb:

Blog::Application.routes.draw do

  mount Api::V1 => "/"
end

当我运行时rake routes,出现以下错误:

uninitialized constant Api
/home/zoloo/code/nomin/config/routes.rb:7:in `block in <top (required)>'
/home/zoloo/.rvm/gems/ruby-1.9.3-p194@rails-3.2.11/gems/actionpack-3.2.11/lib/action_dispatch/routing/route_set.rb:282:in `instance_exec'
/home/zoloo/.rvm/gems/ruby-1.9.3-p194@rails-3.2.11/gems/actionpack-3.2.11/lib/action_dispatch/routing/route_set.rb:282:in `eval_block'
/home/zoloo/.rvm/gems/ruby-1.9.3-p194@rails-3.2.11/gems/actionpack-3.2.11/lib/action_dispatch/routing/route_set.rb:260:in `draw'
/home/zoloo/code/nomin/config/routes.rb:5:in `<top (required)>'
/home/zoloo/.rvm/gems/ruby-1.9.3-p194@rails-3.2.11/gems/railties-3.2.11/lib/rails/application/routes_reloader.rb:40:in `block in load_paths'
/home/zoloo/.rvm/gems/ruby-1.9.3-p194@rails-3.2.11/gems/railties-3.2.11/lib/rails/application/routes_reloader.rb:40:in `each'
/home/zoloo/.rvm/gems/ruby-1.9.3-p194@rails-3.2.11/gems/railties-3.2.11/lib/rails/application/routes_reloader.rb:40:in `load_paths'
/home/zoloo/.rvm/gems/ruby-1.9.3-p194@rails-3.2.11/gems/railties-3.2.11/lib/rails/application/routes_reloader.rb:16:in `reload!'
/home/zoloo/.rvm/gems/ruby-1.9.3-p194@rails-3.2.11/gems/railties-3.2.11/lib/rails/application/routes_reloader.rb:26:in `block in updater'
/home/zoloo/.rvm/gems/ruby-1.9.3-p194@rails-3.2.11/gems/activesupport-3.2.11/lib/active_support/file_update_checker.rb:78:in `call'
/home/zoloo/.rvm/gems/ruby-1.9.3-p194@rails-3.2.11/gems/activesupport-3.2.11/lib/active_support/file_update_checker.rb:78:in `execute'
/home/zoloo/.rvm/gems/ruby-1.9.3-p194@rails-3.2.11/gems/railties-3.2.11/lib/rails/application/routes_reloader.rb:27:in `updater'
/home/zoloo/.rvm/gems/ruby-1.9.3-p194@rails-3.2.11/gems/railties-3.2.11/lib/rails/application/routes_reloader.rb:7:in `execute_if_updated'
/home/zoloo/.rvm/gems/ruby-1.9.3-p194@rails-3.2.11/gems/railties-3.2.11/lib/rails/application/finisher.rb:66:in `block in <module:Finisher>'
/home/zoloo/.rvm/gems/ruby-1.9.3-p194@rails-3.2.11/gems/railties-3.2.11/lib/rails/initializable.rb:30:in `instance_exec'
/home/zoloo/.rvm/gems/ruby-1.9.3-p194@rails-3.2.11/gems/railties-3.2.11/lib/rails/initializable.rb:30:in `run'
/home/zoloo/.rvm/gems/ruby-1.9.3-p194@rails-3.2.11/gems/railties-3.2.11/lib/rails/initializable.rb:55:in `block in run_initializers'
/home/zoloo/.rvm/gems/ruby-1.9.3-p194@rails-3.2.11/gems/railties-3.2.11/lib/rails/initializable.rb:54:in `each'
/home/zoloo/.rvm/gems/ruby-1.9.3-p194@rails-3.2.11/gems/railties-3.2.11/lib/rails/initializable.rb:54:in `run_initializers'
/home/zoloo/.rvm/gems/ruby-1.9.3-p194@rails-3.2.11/gems/railties-3.2.11/lib/rails/application.rb:136:in `initialize!'
/home/zoloo/.rvm/gems/ruby-1.9.3-p194@rails-3.2.11/gems/railties-3.2.11/lib/rails/railtie/configurable.rb:30:in `method_missing'
/home/zoloo/code/nomin/config/environment.rb:5:in `<top (required)>'
/home/zoloo/.rvm/gems/ruby-1.9.3-p194@rails-3.2.11/gems/railties-3.2.11/lib/rails/application.rb:103:in `require'
/home/zoloo/.rvm/gems/ruby-1.9.3-p194@rails-3.2.11/gems/railties-3.2.11/lib/rails/application.rb:103:in `require_environment!'
/home/zoloo/.rvm/gems/ruby-1.9.3-p194@rails-3.2.11/gems/railties-3.2.11/lib/rails/application.rb:297:in `block (2 levels) in initialize_tasks'
Tasks: TOP => routes => environment
(See full trace by running task with --trace)

很明显,Rails 无法加载我的 api。为什么?

4

1 回答 1

7

将自动加载路径更改为:

config.autoload_paths += Dir["#{Rails.root}/app"]

当 require 尝试解析Api::V1时,它会首先查找一个名为api_v1.rb. 那是不存在的。api/v1.rb然后它将在所有自动加载路径中寻找。而且您没有app自动加载路径,但您确实拥有app/api它的所有子文件夹。使用上面的代码,您必须v1.rb放入app/api/api/v1.rb.

于 2013-02-13T19:26:08.947 回答