2010-10-28 60 views
2

我只是想讓鏈接指向「user/1/post/1」。我試過用link_to和不用:方法鍵,並得到了相同的結果。爲什麼這個嵌套資源link_to尋找「摧毀」而不是「show」?

ActionController::RoutingError in Posts#index 

Showing /home/test/rails_apps/test_app/app/views/posts/index.html.erb where line #22 raised: 

No route matches {:action=>"destroy", :controller=>"posts", :user_id=>#<Post id: 1, content: "wtf", user_id: 1, created_at: "2010-10-27 20:46:46", updated_at: "2010-10-27 20:46:46">} 

Extracted source (around line #22): 

22:  <td><%= link_to 'Show', user_post_path(p), :method => :get %></td> 
+0

你可以在第22行發佈更多的上下文嗎? – njorden 2010-10-28 19:58:11

回答

1

小心「:method」參數。它指定一個HTTP方法,而不是操作。

+0

好的。我將它改爲:從:show開始,它仍然在尋找銷燬路線。 – 2010-10-28 19:41:18

+0

我期望它仍然是一個問題,我只是想確保這是*不是造成它的一個問題。我也贊成,因爲我不完全明白爲什麼會發生這種情況。 – TreyE 2010-10-28 19:48:15

0

您正在使用user_post_path(p),我猜p => Post Object

但是這條路線還需要一個用戶對象,因此這種資源將評估爲a post of a user

看一看this

它說

In addition to using the routing helpers, Rails can also create paths and URLs from an array of parameters. For example, suppose you have this set of routes: 

resources :magazines do 
    resources :ads 
end 

When using magazine_ad_path, you can pass in instances of Magazine and Ad instead of the numeric IDs. 
<%= link_to "Ad details", magazine_ad_path(@magazine, @ad) %> 

You can also use url_for with a set of objects, and Rails will automatically determine which route you want: 
<%= link_to "Ad details", url_for([@magazine, @ad]) %> 

希望這會有所幫助。