我想为
Android应用做一个API.当搜索时,我发现
{grape}.我正在关注
this tutorial,但我有一个问题启动Rails服务器:
=> Booting WEBrick => Rails 4.0.2 application starting in development on http://0.0.0.0:80 => Run `rails server -h` for more startup options => Ctrl-C to shutdown server Exiting C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activesupport-4.0.2/lib/act ive_support/dependencies.rb:464:in `load_missing_constant': Unable to autoload c onstant Usuarios,expected C:/Sites/appCerca/app/api/v1/usuarios.rb to define it (LoadError)
我的目录:
app ..api ....api.rb ....v1 ......root.rb ......usuarios.rb
和文件:
#application.rb module AppCerca class Application < Rails::Application config.paths.add "app/api",glob: "**/*.rb" config.autoload_paths += Dir["#{Rails.root}/app/api/*"] end end #routes.rb AppCerca::Application.routes.draw do mount API::Root => '/' [...] #app/api/root.rb module API class Root < Grape::API prefix 'api' mount API::V1::Root end end # app/api/v1/root.rb module API module V1 class Root < Grape::API mount API::V1::Usuarios end end end # app/api/v1/usuarios.rb module API module V1 class Usuarios < Grape::API version 'v1' format :json resource :usuarios do desc "Return list of authors" get do Usuario.all end end end end end
为什么我得到这个错误?我使用的是Ruby 1.9.3p484和Rails-4.0.2.
解决方法
尝试一下
将您的API代码的文件从app / api移动到app / api / api或
在API模块之外移动API类(即删除所有模块API行及其相应的结束语句).
Place API files into
app/api
. Rails expects a subdirectory that matches the name of the Ruby module and a file name that matches the name of the class. In our example,the file name location and directory forTwitter::API
should beapp/api/twitter/api.rb
.
因此,您的API :: Root类的正确位置实际上应该是app / api / api / root.rb.
有了这个变化,你的代码开始,对我在Rails 4.0.2工作正常.