How to extend a RESTful route?

I used the scaffold option to generate a RESTful set of routes to a
Document model. All works as described. Now, I wish to extend the
controller to add a 'publish' action on DocumentController and add a
'publish' route to mimic what the generated 'create' action does. How
do I specify this in the routes.rb file? BTW, I need to keep the
create action as generated.

Well, you could add a 'publish' action to your Document controller and
add a 'publish' route. Have you looked at any Rails tutorials yet?


In your routes.rb:

map.resources :document, :collection => { :publish => :post }

and add your

def publish

in your controller.

You might need to tweak this, but I think what you need is:

    map.login 'publish', :controller => 'documents', :action => 'publish'

Other than creating publish() in your documents_controller.rb, you
shouldn't have to change anything else since you're just adding a
mapping for a new method.

Hi All,

Thanks for you suggestions, even the RTFM ones. I tried two
suggestions, and here are my results. Please excuse the line
wrapping, I copied and pasted directly from the 'rake routes' output.
As you can see below, both suggestions result in mapping the desired
'publish' action into the id parameter of an existing action with a
similar route. This is not what I want. I guess I will have to hand-
code the entire set of routes, I had hoped a way of extending a
resource would be DRYer, but apparently not. Any other suggestions?

(1) routes.rb entry:
map.resources :document_clients, :new => { :publish => :put }

rake routes output:
publish_new_document_client PUT /document_clients/new/publish
(.:format) {:controller=>"document_clients", :action=>"publish"}
        new_document_client GET /document_clients/new
(.:format) {:controller=>"document_clients", :action=>"new"}
       edit_document_client GET /document_clients/:id/edit
(.:format) {:controller=>"document_clients", :action=>"edit"}
            document_client GET /document_clients/:id
(.:format) {:controller=>"document_clients", :action=>"show"}
                            PUT /document_clients/:id
{:controller=>"document_clients", :action=>"update"}
                            DELETE /document_clients/:id
{:controller=>"document_clients", :action=>"destroy"}

actual behavior of controller:
document_clients_controller#new action invoke, with :id => 'publish'
#not what I wanted

(2) routes.rb entry:
map.resources :document_clients, :collection => { :publish => :put }

publish_document_clients PUT /document_clients/publish(.:format)
{:controller=>"document_clients", :action=>"publish"}
        document_clients GET /document_clients(.:format)
{:controller=>"document_clients", :action=>"index"}
                         POST /document_clients(.:format)
{:controller=>"document_clients", :action=>"create"}
     new_document_client GET /document_clients/new(.:format)
{:controller=>"document_clients", :action=>"new"}
    edit_document_client GET /document_clients/:id/edit(.:format)
{:controller=>"document_clients", :action=>"edit"}
         document_client GET /document_clients/:id(.:format)
{:controller=>"document_clients", :action=>"show"}
                         PUT /document_clients/:id(.:format)
{:controller=>"document_clients", :action=>"update"}
                         DELETE /document_clients/:id(.:format)
{:controller=>"document_clients", :action=>"destroy"}

document_clients_controller#show, with :id => 'publish' # again, not
what I wanted

please have a look at the following link.. you will get a clear idea
on routing.