1

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

  1. A binding.pry at the top of the get method is never hit
  2. The expected result is served if I rename the endpoint (i.e. the resource) from cars to trucks. In this case, I have changed nothing except the name of the resource... the class name, filename, and mount point in Base all remain the same.
    • The binding.pry gets hit after the resource rename
  3. If I completely remove the file cars.rb and remove the mounting in base.rb, the app still serves an empty string at the endpoint cars (This indicates that the route is being served from somewhere else, but nothing in /api nor in routes.rb nor in bundle 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
HeyZiko
  • 1,660
  • 15
  • 28
  • Are you using serializers? Also why are both API end points class call Cars? – C dot StrifeVII Oct 21 '16 at 22:11
  • @CdotStrifeVII No special serializers. I'm letting Grape do it's built-in json serialization and I'm using grape-entities for exposing specific fields from the underlying ActiveRecord models. All of this remains static between the 'cars' failing use-case and the 'trucks' succeeding use-case. – HeyZiko Oct 21 '16 at 22:14
  • @CdotStrifeVII The endpoints are the same, demonstrating the failing one (resource cars) and the succeeding one (resource trucks). I'm just trying to show that nothing else changed in the class (including the filename) – HeyZiko Oct 22 '16 at 06:22

0 Answers0