-1

I'm trying to build an admin panel to my rails application, but want to keep my admin controllers away from my other controllers. Is there anyway I can have a admin folder in my app folder which contains controllers just for admin stuff.

Thanks in advance.

James Blond
  • 9
  • 1
  • 2

4 Answers4

4

Yes, sure.

You can put all the admin related controllers in app/controllers/admin/ directory.

K M Rakibul Islam
  • 33,760
  • 12
  • 89
  • 110
1

Yes, you can do this by namespacing your controllers under an admin module.

The easiest way to set this up is to use the rails generator, and prefix your resource with "admin":

rails generate controller admin/user

Type rails g controller for specific helps.

Here's a page from the guide with more info: http://guides.rubyonrails.org/routing.html#controller-namespaces-and-routing

Ryenski
  • 9,582
  • 3
  • 43
  • 47
  • Thanks for the answer, but I was hoping I could have my admin controllers outside the controllers directory completely, so /app/admin instead of /app/controllers/admin – James Blond Nov 23 '16 at 21:33
0

If you want to keep your admin completely separate, you can use an engine. To generate the engine, do:

rails plugin new admin --mountable

Then in your main app's routes file, you can mount the engine with:

mount Admin::Engine => "/admin"

See http://guides.rubyonrails.org/engines.html for complete details on engines.

Ryenski
  • 9,582
  • 3
  • 43
  • 47
0

Thats very simple, usually it makes sense to put those in app/controllers/admin but if you use this, you'll need to use a namespace. Rails will then autoload these classes.

It's a good practice to make an ApplicationController per namespace (I'm calling it base controller) like this:

module Admin
  class BaseController < ApplicationController

  end
end

and here's an exapmle controller:

module Admin
  class ExampleController < Admin::BaseController
    def example
    end
  end
end
siegy22
  • 4,295
  • 3
  • 25
  • 43