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 forTwitter::API
should beapp/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
Getting a Rogue Iteration from My .Each Loop
Hardcoded "Require 'Debug'" Can't Find the Sourcefile
Rubymine: Expected ; or End of Line Error After Some Colon
Instance_Eval Does Not Work with Do/End Block, Only with {}-Blocks
What Is a More Ruby-Like Way of Doing This Command
Update Field Through Link_To in Rails
Can't Dup Nilclass on Association Methods
How to Read a Clients Windows Login Name Using Ruby on Rails
Uploading New Products with Multiple Variant Options
Bootstrap Modal in Ruby on Rails Not Working
Idiomatically Mock Openuri.Open_Uri with Minitest
"Bad Ecpoint" Ssl Error on Fresh Rvm Ruby 1.9.3 Install on Osx Mountain Lion
Rails 3.2 Create a Form That's Used in the Footer of Every Page
When Should I Use Datetime VS Date, Time Fields in Ruby/Rails
Why Should You Avoid the Then Keyword in Ruby
Recursive Rails Nested Resources
Ruby#Index Method VS Binary Search
Why Is Heroku's Heroku-18 Stack Only Compatible with Puma Versions 3.7.X