为什么我不能用Rails和葡萄来“自动加载”?

时间:2022-09-03 23:23:11

I want to do an API for an Android app. When searching, I found {grape}. I'm following this tutorial, but I have a problem launching the Rails server:

我想为Android应用程序做一个API。搜索时,我发现了{葡萄}。我正在学习本教程,但是在启动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)

My directory:

我的目录:

app
..api
....api.rb
....v1
......root.rb
......usuarios.rb

and the files:

和文件:

#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

Why am I getting this error? I am using Ruby 1.9.3p484 and Rails-4.0.2.

为什么会出现这个错误?我使用的是Ruby 1.9.3p484和Rails-4.0.2。

1 个解决方案

#1


20  

Try either

试着要么

  • Moving your API code's files from app/api to app/api/api, or

    将API代码的文件从app/ API移动到app/ API / API,或者

  • Moving your API classes outside the API module (i.e. deleting all the module API lines and their corresponding end statements).

    将API类移到API模块之外(即删除所有模块API行及其相应的结束语句)。

From Grape's documentation:

从葡萄的文档:

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 for Twitter::API should be app/api/twitter/api.rb.

将API文件放入app/ API中。Rails期望一个与Ruby模块名称匹配的子目录和一个与类名相匹配的文件名。在我们的示例中,Twitter::API的文件名位置和目录应该是app/ API / Twitter / API .rb。

Thus the correct location for your API::Root class would actually be app/api/api/root.rb.

因此,您的API的正确位置::Root类实际上是应用程序/ API / API / Root .rb。

With this change your code starts and works fine for me on Rails 4.0.2.

通过此更改,您的代码在Rails 4.0.2上启动并正常工作。

#1


20  

Try either

试着要么

  • Moving your API code's files from app/api to app/api/api, or

    将API代码的文件从app/ API移动到app/ API / API,或者

  • Moving your API classes outside the API module (i.e. deleting all the module API lines and their corresponding end statements).

    将API类移到API模块之外(即删除所有模块API行及其相应的结束语句)。

From Grape's documentation:

从葡萄的文档:

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 for Twitter::API should be app/api/twitter/api.rb.

将API文件放入app/ API中。Rails期望一个与Ruby模块名称匹配的子目录和一个与类名相匹配的文件名。在我们的示例中,Twitter::API的文件名位置和目录应该是app/ API / Twitter / API .rb。

Thus the correct location for your API::Root class would actually be app/api/api/root.rb.

因此,您的API的正确位置::Root类实际上是应用程序/ API / API / Root .rb。

With this change your code starts and works fine for me on Rails 4.0.2.

通过此更改,您的代码在Rails 4.0.2上启动并正常工作。