1

I was just wondering if there was a better way to organize these rails routes. Perhaps in a group? I'm unfamiliar with the best practices of something like this.

TestApp::Application.routes.draw do
  root :to => 'posts#index'
  resources :posts, :except => :destroy
  match '/posts/page/:page' => 'posts#page'
  match '/posts/delete/:id' => 'posts#delete'
  match '/posts/undelete/:id' => 'posts#undelete'
  match '/posts/hide/:id' => 'posts#hide'
  match '/posts/unhide/:id' => 'posts#unhide'
  match '/help' => 'help#index'
end

1 Answer 1

2

Yes, you can group them in a collection block nested within a block on resources :posts as follows

resources :posts, :except => :destroy do
  collection do
    match 'page/:page'   => 'posts#page'
    match 'delete/:id'   => 'posts#delete'
    match 'undelete/:id' => 'posts#undelete'
    match 'hide/:id'     => 'posts#hide'
    match 'unhide/:id'   => 'posts#unhide'
  end
end

These routes aren't very RESTful though. I might suggest something like the following

resources :posts, :except => :destroy do
  member do
    get    'page/:page' => 'posts#page'
    delete 'delete'     => 'posts#delete'
    put    'undelete'   => 'posts#undelete'
    put    'hide'       => 'posts#hide'
    put    'unhide'     => 'posts#unhide'
  end
end

This creates cleaner routes while still pointing to the same controller/action. You will have to modify your application to point to the correct routes a bit differently to support this more accepted behavior though.

              GET    /posts/:id/page/:page(.:format)   posts#page
  delete_post DELETE /posts/:id/delete(.:format)       posts#delete
undelete_post PUT    /posts/:id/undelete(.:format)     posts#undelete
    hide_post PUT    /posts/:id/hide(.:format)         posts#hide
  unhide_post PUT    /posts/:id/unhide(.:format)       posts#unhide
        posts GET    /posts(.:format)                  posts#index
              POST   /posts(.:format)                  posts#create
     new_post GET    /posts/new(.:format)              posts#new
    edit_post GET    /posts/:id/edit(.:format)         posts#edit
         post GET    /posts/:id(.:format)              posts#show
              PUT    /posts/:id(.:format)              posts#update
2
  • Ah, thank you! the latter was exactly what I was looking for. My only question is that you modified the delete action to send an HTTP DELETE, but it doesn't actually delete anything in my tables, it only modifies an is_deleted column in the database. Will that still be okay, or should that be changed to a GET? Dec 20, 2012 at 20:36
  • It definitely should not be a GET. I would personally leave it a DELETE, since from a user's perspective the resource is removed, but others will argue a PUT is more RESTful.
    – deefour
    Dec 20, 2012 at 20:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.