2015-04-17 105 views
0

我有一个表单,它是“评论”对象的一部分,它是“后”对象的孩子,后者又是孩子的“类别”对象,我得到上面的错误消息与下面的代码(使用simple_forms)任何建议?未定义的方法`post_comments_path'为#<#<Class:0xa3ddb8c>:0xb501ae4>

= simple_form_for([@post, @post.comments.build], html: {class: 'form-horizontal'}) do |f| 
= f.input :comment, label: "Your Reply", input_html: {class: "form-control"} 
= f.submit 

的routes.rb:

Rails.application.routes.draw do 
devise_for :users 
resources :categories do 
resources :posts do 
resources :comments 
end 
end 

root 'categories#index' 
end 
+2

请提供你的'配置/ routes.rb'。 –

+2

发布你的config/routes.rb文件的相关部分 –

+0

@Longhanks,请在你的文章中添加路由,而不是在这里。 –

回答

1

您已经定义posts在你comments路线,这是categories下嵌套。有了正确的缩进,该错误很容易看到:

Rails.application.routes.draw do 
    resources :categories do # Everything is nested under here 
    resources :posts do 
     resources :comments 
    end 
    end 
end 

所以,你有一个categories_posts_comments_path

如果在控制台中运行rake routes,您应该会看到所有现有路由的输出。如果你不希望这种行为:

resources :posts do 
    resources :comments 
    end 

    resources :categories do # Everything is nested under here 
    resources :posts 
    end 

但要注意,这将重复很多路线的,所以你要使用的onlyexcept参数限制生成路由的数量。

0

原来我不得不改变连结此:

= simple_form_for([@category, @post, @post.comments.build], html: {class: 'form-horizontal'}) do |f| 

,并改变路线:

Rails.application.routes.draw do 
devise_for :users 
    resources :categories do 
    resources :posts 
    end 
    resources :posts do 
    resources :comments 
    end 

    root 'categories#index' 
end 
+0

谢谢大家的帮助 – Longshanks

相关问题