我试图在 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。为什么?