Почему этот вложенный ресурс link_to ищет «уничтожить» вместо «показать»? - PullRequest
2 голосов
/ 28 октября 2010

Я просто пытаюсь получить ссылку, указывающую на "user / 1 / post / 1".Я попытался использовать link_to с и без ключа: method и получил те же результаты.

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>

Ответы [ 2 ]

1 голос
/ 28 октября 2010

Будьте осторожны с аргументом ": метод".Он указывает метод HTTP, а не действие.

0 голосов
/ 08 сентября 2011

Вы используете user_post_path(p), где, я думаю, p => Post Object

Но для этого маршрута также потребуется пользовательский объект, поэтому этот ресурс оценивается как a post of a user

Посмотрите на это

Там написано

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

Надеюсь, это поможет.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...