Sig
Sig

Reputation: 5916

Adding API endpoint to existing Rails 5 app

I need to add an API endpoint to a Rails (5.0.2) app so I have created a TransitController under app/controllers/api.

The controller, for now, looks like

module Api   
  class TransitController < ActionController::Api
    def create 
      respond_to do |format|
        format.json { render :status => :ok, :nothing => true }
      end
    end
  end
end

I have updated my routes as follows

  namespace :api do
    post 'transits', to: 'transits#create'
  end

But now, when I try to hit the endpoint via curl I get

ActionController::RoutingError (uninitialized constant ActionController::Api)

In Rails 5+, shouldn't ActionController::Api be available by default?

Am I missing anything here?

Thanks

Upvotes: 4

Views: 4439

Answers (1)

Gerry
Gerry

Reputation: 10497

Yes, it is available, but you need to use API instead of Api.

So change this:

class TransitController < ActionController::Api

to:

class TransitController < ActionController::API

More information here.

Upvotes: 6

Related Questions