-1

我有一个路由错误,但我认为我的路由是正确的:

Routing Error

No route matches {:controller=>"posts", :action=>"edit", :user_id=>#<Post id: 9, title: "Na Curva do Horizonte", content: "Eu na mesma minha opinião no pensamento vejo me ca...", created_at: "2013-01-12 20:41:57", updated_at: "2013-01-12 20:41:57", image_file_name: "iris_by_archang3lzz-d5k2i5l.jpg", image_content_type: "image/jpeg", image_file_size: 1101282, image_updated_at: "2013-01-12 20:41:56", user_id: 5>}
Try running rake routes for more information on available routes.

当我执行rake routes时,结果是正确的:

       user_posts GET    /user/:user_id/posts(.:format)              posts#index
                  POST   /user/:user_id/posts(.:format)              posts#create
    new_user_post GET    /user/:user_id/posts/new(.:format)          posts#new
   edit_user_post GET    /user/:user_id/posts/:id/edit(.:format)     posts#edit
        user_post GET    /user/:user_id/posts/:id(.:format)          posts#show

我的路线是:

 resources :posts

  resources :user  do
    resources :posts,:comments
  end

  resources :posts do
    resources :comments
  end

错误的链接是:

<%= link_to 'Edit', edit_user_post_path(notice) %>

我不知道出了什么问题。

4

1 回答 1

2

由于您要编辑属于特定用户的特定帖子,因此您需要将这两个作为参数传递给该链接,将父级放在首位。否则,rails 不知道您要编辑哪个帖子。

<%= link_to 'Edit', edit_user_post_path(@user, @post) %>

所以你似乎只需要这两个变量。

仔细阅读你的 rake 路线。它说:

/user/:user_id/posts/:id/edit(.:format)

所以在那里你可以看到你需要:user_id以及:id,它指的是帖子。将对象作为参数就足够了,rails 足够聪明,可以找出它们的 id 并将它们用于链接。

于 2013-01-12T21:22:36.443 回答