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 Henry ponted

Sijo

Thanks to Nicholas Henry 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

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

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

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

./script/generate model Link

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

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

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