Grape :: API - 无法自动加载常量Base,预期/app/api/v1/base.rb来定义它(LoadError)

时间:2014-09-10 11:29:11

标签: ruby-on-rails grape-api

我需要一些帮助才能让Grape :: API启动并运行Rails 4。 我得到Unable to autoload constant Base,即使puts告诉我该类已加载。 我究竟做错了什么?

应用/ 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 spec / 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

2 个答案:

答案 0 :(得分:1)

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

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

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

答案 1 :(得分:0)

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