2011-07-07 47 views
2

我認爲這將是微不足道的,但無法在任何地方找到它。智能路線的較短網址

我的網站有很多不同的型號,都是相互關聯的。 比方說:

  • 作者
  • 照片

一本書和一張照片都有一個作者,照片可以是一本書裏面。

resources :authors do 
    resources :books 

etc.. 

所以,使用嵌套足智多謀的路線,我們得到的東西,如:

/authors/john-smith/photos/picture-of-a-house 

/books/house-renovation/photos/picture-of-a-house 

這些URL匹配我有我的網站上的麪包屑。 瀏覽路徑是這樣的:

Home > Books > House Renovation > Photos > Picture of a House 

的問題是,這些URL和麪包屑變得太長,太「資源類」。我的意思是,如果使用/ categories/category_id/products/id格式,您可以從遠處發現Rails網站。

有沒有辦法縮短這個,甚至使它變得美麗? 它仍然需要類別和產品名稱,但最多隻有一個參數。 喜歡的東西:

Book_Photos > House Renovation > Picture of a House 

回答

0

您可能能夠通過簡單地在:path選項傳遞給你的資源,並將其設置爲空字符串,在你的路由文件來做到這一點。

我只是打得四處如下:

resources :authors, :path => "" do 
    resources :books, :path => "" 
    end 

運行耙路線在命令行上就產生這樣的:

  author_books GET /:author_id(.:format)   books#index 
          POST /:author_id(.:format)   books#create 
      new_author_book GET /:author_id/new(.:format)  books#new 
     edit_author_book GET /:author_id/:id/edit(.:format) books#edit 
       author_book GET /:author_id/:id(.:format)  books#show 
          PUT /:author_id/:id(.:format)  books#update 
          DELETE /:author_id/:id(.:format)  books#destroy 
        authors GET /        authors#index 
          POST /        authors#create 
       new_author GET /new(.:format)     authors#new 
       edit_author GET /:id/edit(.:format)    authors#edit 
        author GET /:id(.:format)     authors#show 
          PUT /:id(.:format)     authors#update 
          DELETE /:id(.:format)     authors#destroy 

Rails Routing Guide是非常徹底的,當談到解釋什麼可以做到。