I have a controller named BaseController that inherits from ApplicationController whitout a model associated but it has ping method that just respond with a message to inform that everything is OK.
I’m trying to call the action ping through the BaseController setting this in my routes.rb file:
namespace :api, defaults: { format: 'json' } do
match '/ping' => 'base#ping'
end
But it always give me an NameError uninitialized constant Base. I suppose it’s trying to find a model called Base which doesn’t exist so, I don’t know how to set to the correct route to my controller.
The content of my BaseController is the following:
class Api::BaseController < ApplicationController
load_and_authorize_resource
respond_to :json
def ping
respond_with({ :status => 'OK' })
end
end
As extra information: BaseController is just a parent controller for other controllers to inherit. The others are resourceful controllers and have models associated
Thanks.
When you put a namespace around a route, it will look for the controller within that namespace.
So in you case, it will be looking for a controller called Api::BaseController, which normally would be stored in app/controllers/api/base_controller.rb. Is this how your controller is set up?
See here for more details: http://guides.rubyonrails.org/routing.html#controller-namespaces-and-routing
EDIT:
I don’t think its not finding the controller that’s the problem. The error is being caused because you are calling
load_and_authorize_resourcein the controller. CanCan uses the controller name to attempt to load the resource.If there is no model for the controller, make the call
authorize_resource :class => false.See the bottom of this page for more details.