Why Does Trying to Use Grape with Rails Fail with "Uninitialized Constant API"

Rails: Grape API - NameError: uninitialized constant API

Change your root.rb into this:

class Root < Grape::API
prefix 'api'
mount V1::Root
# mount V2::Root (next version)
end

Or, change your directory structure into this:

app > api > api > root.rb

Rails is looking for api folder as you are using module API. But you have already auto loaded api folder in application.rb, so you need another api folder if you keep your root.rb as it is.

Why does trying to use Grape with Rails fail with uninitialized constant API?

It's because app/api is the top-level folder for your API classes, not app.

From 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.

Therefore the correct location for an API::Root class would actually be app/api/api/root.rb, not /app/api/root.rb—though that is the correct location for a class in the top-level namespace, which is why the second example you give (with classes removed from the API module) works.

I recommend you keep your API classes together in their own module, though, and move them to a matching subfolder beneath app/api.

Uninitialized constant in rails 4 w/ grape occuring in deployment only

got same issue. I think autoload_paths and paths configurations is the point.

this is files structure:

app
└── api
   └── API
   ├── V1
   │   └── songs.rb
   └── root.rb

so when I changed config/application.rb to:

module Web
class Application < Rails::Application
config.paths.add File.join('app', 'api'), glob: File.join('**', '**', '*.rb')
config.autoload_paths += Dir[Rails.root.join('app', 'api', '**', '**', '*')]
# ... etc
end
end

everything just worked.

Uninitialized constant API on rake routes

As per the discussion, you need to update your grape gem's version to 0.9.0 and then you need to add this line in your Gemfile:

gem 'grape', '0.9.0'

and then:

$ bundle install

Rails, Grape entity throws: uninitialized constant Grape::Entity

You are using old version of grape, change your grape version:

gem 'grape', '~> 0.11.0'

block in load_missing_constant': uninitialized constant API::V1::Users (NameError)

This mainly occurs when naming conventions doesn't match. Try changing api/v1/user.rb to api/v1/users.rb which also the blog suggests. You missed a 's'.



Related Topics



Leave a reply



Submit