Problem
I have a Rails application with a Grape API implementation. For the most part, everything works well. However, I have a peculiar case that an endpoint named cars
always serves up an empty string, the code for the get
method is never executed, but the route seems to be recognized because there is no route error.
Observations
- A
binding.pry
at the top of theget
method is never hit - The expected result is served if I rename the endpoint (i.e. the resource) from
cars
totrucks
. In this case, I have changed nothing except the name of the resource... the class name, filename, and mount point inBase
all remain the same.- The
binding.pry
gets hit after the resource rename
- The
- If I completely remove the file
cars.rb
and remove the mounting inbase.rb
, the app still serves an empty string at the endpointcars
(This indicates that the route is being served from somewhere else, but nothing in/api
nor inroutes.rb
nor inbundle exec rake routes
seems to indicate this.)
Reference
/app/api/v2/cars.rb (serves empty string at /api/v2/cars
)
module V2
class Cars < Grape::API
resource :cars do
get do
cars = Car.all
present cars, with: V2::Entities::Car
end
end
end
end
/app/api/v2/cars.rb (serves expected result at /api/v2/trucks
)
module V2
class Cars < Grape::API
resource :trucks do # Renamed from cars to trucks
get do
cars = Car.all
present cars, with: V2::Entities::Car
end
end
end
end
routes.rb (simplified for this post)
Rails.application.routes.draw do
namespace :inventory do
resources :assets do
collection do
get :scan
end
end
end
resources :vehicles do
resources :cars, except: [:index, :show, :edit]
end
get 'home/index'
get 'home/inventory'
get 'home/vehicles'
devise_for :users
root to: 'home#index'
mount V2::Base => '/api'
end