Controller subfolders and routing

Greetings folks,

I’m looking for some confirmation before I go further astray.

I decided to anticipate future growth and rather than use a flat
controller directory (app/controllers ) without subfolders , I would
try a ‘tree’ structure with subfolders (app/controllers/admin, app/
controllers/info, etc.).

Duh, but I ended up with broken routes. The fix seems to be to add
all the necessary permutations of subdirectory routes to the routes.rb
file. This of course leads to a fate potentially worse than the basic
flat directory.

Am I missing something easier and more generic to accommodate the
routing for ‘tree’ directories.?

Thanks. Bill

Check that you have done the following (using admin/
products_controller.rb as an example):

1. Namespace your routes in routes.rb

map.namespace :admin do |admin|
admin.resources :products
admin.resources :orders

add other resources here

end

2. Namespace your controllers

class Admin::Products < ApplicationController
end

HTH,
Nicholas

This can be done like
./script/generate controller Admin::Products
and the rest as Nicholas H. ponted

Sijo

Thanks to Nicholas H. and Sijo Kg for responding.

Having to add the routes to the routes.rb file still seems a
cumbersome approach, but so be it. Unfortunately it isn’t working as
I raise a Routing Error ( No route matches “/link” with
{:method=>:get} ) with the following configuration


ActionController::Routing::Routes.draw do |map|
map.namespace :info do |info|
info.resources :citation
info.resources :download
info.resources :link
info.resources :recommendation
end

Install the default routes as the lowest priority.

map.connect ‘:controller/:action/:id’
map.connect ‘:controller/:action/:id.:format’
end


class Info::LinkController < ApplicationController
def index
@title = “LINKS”
end
end

  • <%= link_to “Links”, {:controller =>‘link’, :action =>‘index’ }
    %>
  • Well there’s one benefit of having it defined in the routes.rb now –
    you have nice helper methods for the URL’s :slight_smile:

    Some suggests:

    Pluralize your routes


    ActionController::Routing::Routes.draw do |map|
    map.namespace :info do |info|
    info.resources :citations
    info.resources :downloads
    info.resources :links
    info.resources :recommendations
    end

    Install the default routes as the lowest priority.

    map.connect ‘:controller/:action/:id’
    map.connect ‘:controller/:action/:id.:format’
    end

    Pluralize your controller

    class Info::LinksController < ApplicationController
    def index
    @title = “LINKS”
    end
    end

    Use a helper method for the url


  • <%= link_to "Links", info_links_path %>
  • ---------

    You can see what routes have been defined by executing the following
    rake command in your application:

    $ rake routes

    May I suggest you read up on Restful routing on the Rails Guide
    (free):

    Peepcode also has a great screencast on the subject (not free):

    http://peepcode.com/products/rest-for-rails-2

    Cheers!
    Nicholas

    You don’t need to namespace the model as well.

    ./script/generate model Admin::Link (not necessary)

    ./script/generate model Link

    Hi zambezi
    When create a controller do like
    ./script/generate controller Admin::Links

    Note : plural for controller

    If you are scaffolding it will be
    

    ./script/generate scaffold Admin::Link
    Note: It is singular

    And if generate model then
    

    ./script/generate model Admin::Link

    Note: Here also singular
    And now change the routes
    Sijo

    And if model is also namespaced the table will be admin_links
    When do
    ./script/generate scaffold Admin::Link
    happens this

    Sijo

    Another thing to add to this is Now in the model
    Admin::Link < ActiveRecord::Base have to say like

    set_table_name :admin_links Then we can access it like
    Admin::Link.find(:all) etc

    Sijo

    I would avoid ever using a hash to specify a path. Always use named
    routes.

    If this is your root of your application then try this:

    use root_path in your views

    map.root :controller => “home” # :action => “index” is implied

    or if not, then create a named route

    use home_path in your views

    map.home ‘home’, :controller => “home” # again :action => “index” is
    implied

    At the end of the day you should remove the following from your routes
    file:

    map.connect ‘:controller/:action/:id’
    map.connect ‘:controller/:action/:id.:format’

    So you never rely on the :controller/:action has in your views. Always
    use named_routes.

    HTH,
    Nicholas

    Thank you Sijo and Nicholas,

    Your much appreciated replies have given me some insight and got me
    moving again. The RESTful reading recommendation (RailsGuide) is
    excellent and has shed considerable light on what was a murky subject
    for me.

    However, one further question regarding the link_to helper.

    Since Rails conserves paths (and breaks routes when mapping
    namespaces), I amended the old style link_to method by adding a
    forward slash in front of the controller name (‘home’ to ‘/home’).

    <%= link_to “Home”, {:controller =>’/home’, :action =>‘index’ } %>

    How do I achieve this using the new, improved RESTful helper style?

    Cheers, Bill