加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 为什么我用Rails和葡萄获得“无法自动加载常量

发布时间:2020-12-16 19:42:26 所属栏目:百科 来源:网络整理
导读:我想为 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 op
我想为 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行及其相应的结束语句).

从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 :: Root类的正确位置实际上应该是app / api / api / root.rb.

有了这个变化,你的代码开始,对我在Rails 4.0.2工作正常.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读