Grape :: API - Unable to autoload permanent database, expected / app / api / v 1 / base.rb define it (LoadError)

I need help to run the Grape :: API with Rails 4. I get Unable to autoload constant Base , although puts tells me that the class is loaded. What am I doing wrong?

application /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 

application / API / v 1 / 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)>' 

specifications / 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 

The Versions I Use

 $ 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 
+5
source share
2 answers

Try Base inherit from Grape::API instead of API :

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

Having inherited from the API , you create a circular dependency: the interpreter cannot know the definition of V1::Base until it recognizes the definition of the API , but for this it first needs to know the definition of V1::Base , etc.

+1
source

Change to mount ::V1::Base fixed.

0
source

Source: https://habr.com/ru/post/1202231/


All Articles