Namespaced routing

Hello Rails Talk people

(I am using rails 2.3.5, with ruby 1.8.7) When I     ruby script/generate scaffold admin/category name:string or     ruby script/generate scaffold Admin::category name:string and then     rake db:migrate:up VERSION=20091206005933 whatever... and configure in routes.rb   map.namespace :admin do |admin|     admin.resources :categories   end then, when going to     http://www.domain.com/appname/admin/categories then following error is produced:     Mysql::Error: Table 'sr22x3_development.categories' doesn't exist: SELECT * FROM `categories` Now, of course this error is produced, because, after all, there is indeed NO table named "categories". That's because the database that the migrations produced is named     admin_categories, not     categories. Unless I go through a massive renaming of components, such as suggested by     RESTful admin namespaced controller using scaffolding • AxiomQ Ventures then nothing works. (Slobodan's fix, from his site, is given below.)

I thought that subdirectory and Namespace routing was supposed to be working.

Regards, all Maurice Yarrow

Slobodan's fix, from his site, is quoted below:

Here is the easiest way I found so far to accomplish this. This example generates categories model and controllers for it.

./script/generate controller admin/categories ./script/generate scaffold category name:string

This will generate an empty controller in admin namespace and a scaffolded resource for front-end controller.

Now we have everything generated we just need to make it work with admin controller and not with front-end.

move all templates from app/views/categories to app/views/admin/ categories copy all functions from categories_controller.rb to admin/ categories_controller.rb add namespace for admin controller in routes.rb: map.namespace :admin do |admin|   admin.resources :categories end In admin/categories_controller.rb replace in 3 places redirect_to calls to work with admin namespace. It will have something like redirect_to(@category), but to work with namespace it needs to have redirect_to([:admin, @category]) make similar changes in all templates, i.e. make it work within an admin namespace. You need to make following changes: form_for(@category) => form_for([:admin, @category]) <%= link_to ‘Show’, @category %> => <%= link_to ‘Show’, [:admin, @category] %> categories_path => admin_categories_path edit_category_path(@category) => edit_admin_category_path(@category) new_category_path => new_admin_category_path That’s it. Now you’ll have /admin/categories for all administrative tasks and you have a free controller for front-end actions. You might wonder why not just generate scaffold for admin/categories… The reason is that you’ll also get a model that is namespaced in admin (i.e. model would be Admin::Category). Scaffolded views also wouldn’t work as it seems that generator doesn’t take into account the fact that you are using a namespace.

Hello further, Rails Talk people

First: correcting a typo on line 20: (line 20:) 'named "categories". That's because the database that the migrations' The word 'database' in the above should (obviously) have been 'table'.

Second: Concerning Namespaced and subdirectory scaffold generation and routing: Looking further at various blogs, I see that in     http://railsforum.com/viewtopic.php?id=16401 duzzell wrote: I'm having the same problem. The scaffold generator doesn't seem to correctly distinquish between namespaces and nested models. I generate a scaffold in a namespace the same way you do. Then I have to: 1 move the model.rb file to it's parent directory 2 remove the namespace:: prefix from the table name in the migration file 3 modify references to the table in the controller to remove the namespace:: prefix 4 modify the resources statement in the routes.rb file to put the new model in the namespace 5 modify the paths in the views to include the namespace_ prefix

This above approach, which removes the namespace prefix from everywhere it shouldn't have been, is the complementary approach to Slobodan's, which adds the namespace prefix everywhere it should have been but wasn't. (Depending, of course, what one believes to be the appropriate approach.)

Third: Scanning to the bottom of Slobodan's article from September, 2008, I see blog responses dating till as recently as August, 2009, which all tend to imply that the Rails 'generate scaffold admin/category' result had not been made consistent till at least Aug, 09. Doing a search on the issue in the Rails Talk group, there is a post about this just a day before mine,

http://groups.google.com/group/rubyonrails-talk/browse_thread/thread/6c84c5529185470d/c95007e4c2d0796a?lnk=gst&q=scaffold+for+namespace+routes#c95007e4c2d0796a

So, Namespaced and subdir scaffold generation appears to still be a broken functionality. (The routing does not appear to be an issue here.) So, at this point, I am willing to believe that this is actually a Rails bug of sorts I didn't see a search for the Developer bug-fix list at     Overview - Ruby on Rails - rails so I couldn't ascertain if this is an open issue.

Maurice Yarrow