Sub-directories on rails
I haven’t been able to find this anywhere in literature (I’m sure it is somewhere, I just haven’t figured a good enough google search string to find it), but I wanted to be able to use sub directories to partition different areas in Ruby on Rails.
e.g /admin /admin/articles etc
Ruby on rails uses the following mappings between the URL and the controller:
http://www.url.com/application/controller/action/id
which maps to a AcitonController class called controller which has a method called action
I wanted to have multiple a controller directories:
http://www.url.com/application/controller/subcontroller/action/id
You do this by creating a sub directory under the controller directory (in this case called controller) and creating file called subcontroller_controller.rb – The class declaration of this file should be Controller::SubControllerController < ApplicationController.
Â
So, if you wanted to create a controller called articles in the sub directory admin you would create:
- The subdirectory “admin” under the controllers directory
- The file articles_controller.rb in the controllers/admin directory
- Declare the class as: Admin::ArticlesController < ApplicationController
You also need to create the corresponding .rhtml files in a sub directory in the views folder…