0

我正在做一个小任务来创建帖子并按类别划分它们。我一切正常,但在我的 index.html 中,我收到以下与我的链接相关的路由错误。

No route matches {:action=>"show", :controller=>"posts"}
No route matches {:action=>"edit", :controller=>"posts"}
undefined method `post_path' for #<#<Class:0x007fd3097f0ce0>:0x007fd3097c9370>

在 posts/index.html.haml 我有:

    - @category.posts.each do |post|
        %tr
          %td= post.title
          %td= post.description
          %td= post.user_id
          %td= post.category_id
          %td= link_to 'Show', category_post_path //gives first error
          %td= link_to 'Edit', edit_category_post_path //gives second error
          %td= link_to 'Destroy', post, 
          :confirm => 'Are you sure?', :method => :delete //gives third error

在 routes.rb 我有:

resources :categories do
    resources :posts
  end

当我运行 rake 路线时,我得到:

categories_index  GET    /categories/index(.:format)    categories#index                
category_posts    GET    /categories/:category_id/posts(.:format)     posts#index
                  POST   /categories/:category_id/posts(.:format)          posts#create
new_category_post GET    /categories/:category_id/posts/new(.:format)      posts#new
edit_category_postGET    /categories/:category_id/posts/:id/edit(.:format) posts#edit
category_post     GET    /categories/:category_id/posts/:id(.:format)      posts#show
                  PUT    /categories/:category_id/posts/:id(.:format)      posts#update
                  DELETE /categories/:category_id/posts/:id(.:format)      posts#destroy

我的索引中有问题导致应用程序崩溃,因为我可以毫无问题地访问和查看这些内容:

/categories/:category_id/posts/:id (equivalent to show)
/categories/:category_id/posts/:id/edit (equivalent to edit)

有人可以帮我吗?先感谢您。

4

1 回答 1

1

URL 助手需要知道您对哪个类别和帖子感兴趣,因此您必须将特定类别和帖子对象作为参数传递给助手。我认为这些应该有效:

      %td= link_to 'Show', category_post_path(@category,post) //gives first error
      %td= link_to 'Edit', edit_category_post_path(@category,post) //gives second error
于 2012-05-17T21:10:59.253 回答