开发者

Why is this nested resource link_to looking for "destroy" instead of "show"?

开发者 https://www.devze.com 2023-01-23 01:21 出处:网络
I\'m just trying to get the link to point to \"user/1/post/1\".I\'ve tried using the link_to with and without the :method key and got the same results.

I'm just trying to get the link to point to "user/1/post/1". I've tried using the link_to with and without the :method key and got the same results.

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开发者_StackOverflow中文版>


Be careful with the ":method" argument. It specifies an HTTP method, not the action.


You are using user_post_path(p) where I guess p => Post Object

But this route will also need a user object, hence this resource will evaluate to a post of a user

Have a look at this

It says

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]) %>

Hope this helps.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号