Rails中的未定义方法`category_documentations_path'。我的路线有评论而不是文件

时间:2018-03-28 20:12:17

标签: ruby-on-rails routes nested-forms

我尝试使用嵌套表单,当我加载表单(/categories/show.html.haml)时,我收到此错误:

NoMethodError in Categories#show

Showing /home/cederic/rails/mordus/app/views/categories/show.html.haml where line #6 raised:

undefined method `category_documentations_path' for #<#<Class:0x00007fead8742870>:0x00007fead8881038>
Did you mean?  category_comments_path

配置/ routes.rb中

Rails.application.routes.draw do
  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html

    resources :categories do
        resources :documentations
    end

    get 'pages/accueil'

    root 'pages#accueil'
end

“rails routes”命令的输出:

refix Verb   URI Pattern                                          Controller#Action
    category_comments GET    /categories/:category_id/comments(.:format)          comments#index
                      POST   /categories/:category_id/comments(.:format)          comments#create
 new_category_comment GET    /categories/:category_id/comments/new(.:format)      comments#new
edit_category_comment GET    /categories/:category_id/comments/:id/edit(.:format) comments#edit
     category_comment GET    /categories/:category_id/comments/:id(.:format)      comments#show
                      PATCH  /categories/:category_id/comments/:id(.:format)      comments#update
                      PUT    /categories/:category_id/comments/:id(.:format)      comments#update
                      DELETE /categories/:category_id/comments/:id(.:format)      comments#destroy
           categories GET    /categories(.:format)                                categories#index
                      POST   /categories(.:format)                                categories#create
         new_category GET    /categories/new(.:format)                            categories#new
        edit_category GET    /categories/:id/edit(.:format)                       categories#edit
             category GET    /categories/:id(.:format)                            categories#show
                      PATCH  /categories/:id(.:format)                            categories#update
                      PUT    /categories/:id(.:format)                            categories#update
                      DELETE /categories/:id(.:format)                            categories#destroy
           pages_home GET    /pages/home(.:format)                                pages#home
                 root GET    /                                                    pages#home

2 个答案:

答案 0 :(得分:1)

找到它。这是一个奇怪的错误..不知道它是否来自我的文本编辑器或其他什么,但我复制routes.rb的内容,删除它,创建一个新的routes.rb文件并粘贴内容,现在它的工作原理。

答案 1 :(得分:0)

这些资源路由与'rails routes'的输出不匹配 - 你运行'rails routes'后是否更改了routes.rb文件?如果您的routes.rb看起来如上所述,'rails routes'应该返回以下内容(使用'documentations'而不是'comments'):

    category_documentations GET /categories/:category_id/documentations(.:format) comments#index
    category_documentations POST /categories/:category_id/documentations(.:format) comments#create
    ...