Grape: Unable to autoload constant Base, expected app/api/v1/base.rb to define it (LoadError)

Created on 10 Sep 2014  路  6Comments  路  Source: ruby-grape/grape

Hi,

I get a Unable to autoload constant Base even though a puts tells me that the class is loaded.
What am I doing wrong?

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

app/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)>'

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

Most helpful comment

Fixed by using mount ::V1::Base

All 6 comments

You have to make sure this path is in autoload paths or something like that. Check out how things are setup in https://github.com/dblock/grape-on-rails/blob/84e623d6957189ebb8d4d54f350ee37c2295cab2/config/application.rb#L28.

If you can't figure it out, post the whole project somewhere.

Please use the mailing list for questions.

my application.rb

    config.paths.add File.join('app', 'api'), glob: File.join('**', '*.rb')
    config.autoload_paths += Dir[Rails.root.join('app', 'api', '*')]

I added you as a collaborator to my repo. Please see the feature/api_grape branch. I've spent the entire day trying to get Grap installed. I would _really_ appreciate it if you could take a look! :-)

@dblock, I stripped down my code and published it here: https://github.com/stabenfeldt/rails4-grape
Thanks for the tips regarding the mailing-list.
Reposted this issue there: https://groups.google.com/forum/?fromgroups#!topic/ruby-grape/CR8ZifuNo2k

Fixed by using mount ::V1::Base

:thumbsup: , thanks @dm1try for the suggestion!

problem with Base? when I change to another name, it works.

Was this page helpful?
0 / 5 - 0 ratings