2010-08-19 53 views
0

我的路線應該是這樣的:這些導軌路徑應該如何看起來像?

>rake routes 

         GET /categories/:category_id/articles(.:format)   {:controller=>"articles", :action=>"index"} 
    category_articles POST /categories/:category_id/articles(.:format)   {:controller=>"articles", :action=>"create"} 
new_category_article GET /categories/:category_id/articles/new(.:format)  {:controller=>"articles", :action=>"new"} 
         GET /categories/:category_id/articles/:id(.:format)  {:controller=>"articles", :action=>"show"} 
         PUT /categories/:category_id/articles/:id(.:format)  {:controller=>"articles", :action=>"update"} 
    category_article DELETE /categories/:category_id/articles/:id(.:format)  {:controller=>"articles", :action=>"destroy"} 
edit_category_article GET /categories/:category_id/articles/:id/edit(.:format) {:controller=>"articles", :action=>"edit"} 
         GET /categories(.:format)        {:controller=>"categories", :action=>"index"} 
      categories POST /categories(.:format)        {:controller=>"categories", :action=>"create"} 
     new_category GET /categories/new(.:format)       {:controller=>"categories", :action=>"new"} 
         GET /categories/:id(.:format)       {:controller=>"categories", :action=>"show"} 
         PUT /categories/:id(.:format)       {:controller=>"categories", :action=>"update"} 
      category DELETE /categories/:id(.:format)       {:controller=>"categories", :action=>"destroy"} 
     edit_category GET /categories/:id/edit(.:format)      {:controller=>"categories", :action=>"edit"} 

,但每當我嘗試,文章,使用以外的任何其他 new_category_article_path,

<%= link_to 'Show', categories_article %> 

我得到這意味着例外:

undefined local variable or method 'categories_article' for #<#<Class:0x00000102ce5768>:0x00000102ce3a80> 

這是什麼我在這裏做錯了?

回答

2

有兩件事情會在這裏:

  1. 有沒有路由定義則名爲「categories_article」。您可能正在尋找「category_articles」或「category_article」。
  2. 對於「顯示」操作,您需要指定要顯示的對象的ID。在這種情況下,您還需要指定父ID(category_id)
  3. 當按名稱引用這些路由時,需要在末尾添加「_path」或「_url」。

這三個在一起做一樣的東西:

<%= link_to 'Show', category_articles_path(:category_id => category.id) %> 

<%= link_to 'Show', category_article_path(:category_id => category.id, :id => article.id) %> 

,或者更短:

<%= link_to 'Show', [category, article] %> 
+0

非常感謝,mattyven! – Jan 2010-08-19 11:19:22

+0

只是還有一件事 - 是否有一個語法來做其他動作的最短版本,比如編輯? – Jan 2010-08-19 11:31:22

+0

<%= link_to'Show',[a.category,a]%>給出與<%= link_to'Edit',[a.category,a],:action =>「edit」%>相同的結果,我從文檔中查找 – Jan 2010-08-19 11:42:46

相關問題