Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Route for a controller without model in rails 3

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.

like image 981
John Avatar asked Oct 11 '12 08:10

John


People also ask

How many types of routes are there in Rails?

Rails RESTful Design which creates seven routes all mapping to the user controller. Rails also allows you to define multiple resources in one line.

How do I find routes in Rails?

Decoding the http request TIP: If you ever want to list all the routes of your application you can use rails routes on your terminal and if you want to list routes of a specific resource, you can use rails routes | grep hotel . This will list all the routes of Hotel.


1 Answers

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_resource in 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.

like image 88
dnatoli Avatar answered Oct 24 '22 01:10

dnatoli