2015-04-17 68 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

相關問題