5

我需要一些帮助来启动 Grape::API 并使用 Rails 4 运行。Unable to autoload constant Base即使 aputs告诉我类已加载,我也会得到 a。我究竟做错了什么?

应用程序/api/api.rb

class API < Grape::API
  prefix 'api'
  format :json
  default_format :json
  mount V1::Base   # Everything loads perfectly until I add this line.
end

应用程序/api/v1/base.rb

module V1
  class Base < API
    puts "=== DEBUG - in Base"
    version 'v1', using: :path, vendor: 'orwapp', cascade: false

    mount Users

  end
end

$ rspec 规范/api

12:58:29 - INFO - Run all
12:58:29 - INFO - Running all specs
=== DEBUG - in Base
/dependencies.rb:481:in `load_missing_constant': 
Unable to autoload constant Base,
 expected /Users/martins/Work/myapp/app/api/v1/base.rb to define it (LoadError)
        from /Users/martins/Work/myapp/app/api/api.rb:9:in `<class:API>'
        from /Users/martins/Work/myapp/app/api/api.rb:3:in `<top (required)>'

规范/api/users_spec.rb

describe 'GET /api/v1/users/:id', focus: true do
  let(:user) { Fabricate :user }

  it 'returns that specific user' do
    get "/api/v1/users/#{ user.id }", {}, https_and_authorization
    response.status.should eq 200
    parse_response_for(:user)['email'].should eq user.email
  end
end

我正在使用的版本

$ ack grape Gemfile.lock
      remote: git://github.com/intridea/grape.git
        grape (0.9.1)
        grape-entity (0.4.4)
        grape-swagger (0.8.0)
          grape
          grape-entity
4

2 回答 2

1

尝试Base继承 fromGrape::API而不是API

module V1
  class Base < Grape::API
  ...

通过让它从API你那里继承来创建一个循环依赖:解释器在知道 的定义V1::Base之前不能知道 的定义API,但为此它首先需要知道 的定义V1::Base,依此类推。

于 2014-09-10T13:18:48.593 回答
0

更改为 mount ::V1::Base修复它。

于 2014-09-11T08:22:54.973 回答