Topic: routes.rb question

Hi guys...

I've stumbles across this post which lead to this artical for creating RESTful admin sections.

In short you use one controller for admin and public acces but use routes appended with admin to display admin layout.

For some odd reason I cannot get it to work properly. I getting an Unknown action error when trying to access or but and works as expected.

I suspect the problem lies in my routes.rb but i can't figure it out

map.resource :admin do |admin|
  admin.resources :products, :name_prefix => 'admin_', :path_prefix => 'admin'
map.resources :products

Can anyone suggest a solution or tell me where I might have gone wrong...

Thanx guys

Re: routes.rb question

I think I've found the solution...
This might not be the most DRY solution, but it does the trick.

ActionController::Routing::Routes.draw do |map|
  map.connect 'admin/products/:action/:id.:format', :controller=>'products'
  map.connect 'admin/products/:action/:id', :controller=>'products'

  # Install the default route as the lowest priority.
  map.connect ':controller/:action/:id.:format'
  map.connect ':controller/:action/:id'

If anyone has a better solution please point me in the right direction

Last edited by sonic921 (2007-07-18 09:09:56)

Re: routes.rb question

"view_products" is no standard CRUD method that a RESTful Route knows by default (index,show,new,create,edit,update,delete).

you would have to add it to the route manually:

map.resource :admin do |admin|  
  admin.resources :products,
                  :name_prefix => 'admin_',
                  :path_prefix => 'admin'
                  :collection => { :view_products => :get }

map.resources :products,
              :collection => { :view_products => :get }

:collection is for custom actions that aim at a collection of items (here: products)
:member works the same way, but is for actions aiming on a single item (here: one specific product whose id is passed in URL)

the passed paremeter (here: ":get") tells map.resources what HTTP verb (GET/PUT/POST/DELETE) the route should match to.

The Resulting Routes would look like:;view_products

custom methods are appended to the URL with a semicolon (;) as seperator. you can see that with a "new" or "edit" action too as those are no "real" CRUD methods, but built-in in RESTful Routes as one always needs them.
I think the semicolon was replaced with something else in edge rails, though

Last edited by Duplex (2007-07-18 09:47:51)

Re: routes.rb question

AAAAHHH /lighbulb

Thanx dude, this makes tons of sense. Didn't understand it completely till now.

You learn something new everyday ^^