Rails 4.0.4 中的“#<#<Class:0x4e6ec28>:0x4e6e3d0> 的未定义方法`post_comments_path'”

use*_*822 0 ruby ruby-on-rails

undefined method `post_comments_path' for #<#<Class:0x4e6ec28>:0x4e6e3d0>
Run Code Online (Sandbox Code Playgroud)

我在创建与帖子相关的评论时收到此错误。

<%= form_for([@post, @post.comments.build]) do |f| %>
Run Code Online (Sandbox Code Playgroud)

以上是我收到错误的表格部分..

我在评论控制器中的代码:

def create
  @post = Post.find (params[:post_id])
  @comment = @post.comments.create(params[:comments].permit(:commenter, :body))
  redirect_to post_path(@post)
end
Run Code Online (Sandbox Code Playgroud)

请指出我错在哪里。我是 ruby​​ on rails 的新手。

我的路线.rb:

Blog::Application.routes.draw do
  resources :posts 
  resources :comments
  root to:"welcome#index"
  get "welcome/index"
Run Code Online (Sandbox Code Playgroud)

我的佣金路线:

        posts GET    /posts(.:format)             posts#index
              POST   /posts(.:format)             posts#create
     new_post GET    /posts/new(.:format)         posts#new
    edit_post GET    /posts/:id/edit(.:format)    posts#edit
         post GET    /posts/:id(.:format)         posts#show
              PATCH  /posts/:id(.:format)         posts#update
              PUT    /posts/:id(.:format)         posts#update
              DELETE /posts/:id(.:format)         posts#destroy
     comments GET    /comments(.:format)          comments#index
              POST   /comments(.:format)          comments#create
  new_comment GET    /comments/new(.:format)      comments#new
 edit_comment GET    /comments/:id/edit(.:format) comments#edit
      comment GET    /comments/:id(.:format)      comments#show
              PATCH  /comments/:id(.:format)      comments#update
              PUT    /comments/:id(.:format)      comments#update
              DELETE /comments/:id(.:format)      comments#destroy
         root GET    /                            welcome#index
welcome_index GET    /welcome/index(.:format)     welcome#index
Run Code Online (Sandbox Code Playgroud)

我编辑的 routes.rb ::

Blog::Application.routes.draw do
  resources :post  do
    resources :comments
  end
  root to:"welcome#index"
  get "welcome/index"
end
Run Code Online (Sandbox Code Playgroud)

Mar*_*pka 6

您尝试使用嵌套资源,但您没有在routes.rb. 你应该有:

resources :posts do
  resources :comments
end
Run Code Online (Sandbox Code Playgroud)

这应该有效。