1

我只是想知道是否有更好的方法来组织这些铁路路线。也许在一个组?我不熟悉这样的最佳实践。

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
4

1 回答 1

2

的,您可以将它们分组到collection嵌套在块中的块中resources :posts,如下所示

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

这些路由虽然不是很 RESTful。我可能会建议以下内容

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

这会创建更清晰的路线,同时仍指向相同的控制器/动作。您不得不修改您的应用程序以稍微不同地指向正确的路由,以支持这种更可接受的行为。

              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
于 2012-12-20T20:28:50.517 回答