【问题标题】:undefined method `comments_path' which I can't find in the code files我在代码文件中找不到的未定义方法“comments_path”
【发布时间】:2017-09-02 18:38:05
【问题描述】:

我在尝试向文章添加新评论时收到以下错误 (NoMethodError),问题是它引用了我在代码文件中找不到的未定义方法 `cmets_path'

请帮忙

注意: 我试图搜索这个错误,但我发现的结果不相关,问题是错误指向我找不到的东西。

错误如下图:

评论中的 NoMethodError#new 显示 /home/abc/my_ruby_projects/myblog3/app/views/cmets/_form.html.erb 其中第 1 行提出:

# 的未定义方法 `cmets_path' 你的意思?字体路径 提取的源代码(第 1 行附近):

<%= form_with model: @comment do |form| %>
  <% if comment.errors.any? %>
    <div id="error_explanation">
      <....>
      <ul>

模板包含的痕迹:app/views/cmets/new.html.erb

Rails.root:/home/abc/.../myblog3

我已经为文章和 cmets 定义了嵌套路由,如下所示:

  resources :articles do
    resources :comments
  end

我的路线似乎是正确的,如下所示:

          Prefix Verb   URI Pattern                                       Controller#Action
         rails_admin        /admin                                            RailsAdmin::Engine
    article_comments GET    /articles/:article_id/comments(.:format)          comments#index
                     POST   /articles/:article_id/comments(.:format)          comments#create
 new_article_comment GET    /articles/:article_id/comments/new(.:format)      comments#new
edit_article_comment GET    /articles/:article_id/comments/:id/edit(.:format) comments#edit
     article_comment GET    /articles/:article_id/comments/:id(.:format)      comments#show
                     PATCH  /articles/:article_id/comments/:id(.:format)      comments#update
                     PUT    /articles/:article_id/comments/:id(.:format)      comments#update
                     DELETE /articles/:article_id/comments/:id(.:format)      comments#destroy
            articles GET    /articles(.:format)                               articles#index
                     POST   /articles(.:format)                               articles#create
         new_article GET    /articles/new(.:format)                           articles#new
        edit_article GET    /articles/:id/edit(.:format)                      articles#edit
             article GET    /articles/:id(.:format)                           articles#show
                     PATCH  /articles/:id(.:format)                           articles#update
                     PUT    /articles/:id(.:format)                           articles#update
                     DELETE /articles/:id(.:format)                           articles#destroy

【问题讨论】:

    标签: ruby-on-rails ruby


    【解决方案1】:

    您的表单正在寻找可以发布评论更新的路径。

    您的路线配置可能需要类似

    resources :comments
    

    现在,对列出路由的命令行调用将向您显示它正在寻找的 cmets_path

    rake routes
    

    然后你需要一个带有更新方法/动作的 CommentsController 来处理回发。

    class CommentsController
      def edit
      end
    
      def update
        # save data
      end
    end
    

    【讨论】:

    • 感谢您的帮助。实际上我有资源 :cmets 已经在路由中并且控制器有动作,这里的问题主要是新评论而不是显示评论方法。请查看我的 routes.rb 和 rails routes cmd 的更新信息。
    • 您的部分似乎将@comment 与评论混合在一起(没有@)...您的外部视图定义了一个评论变量,还是应该也是@comment?
    • 我想也许你把事情搞混了......你的路线设置为访问 cmets 作为嵌套资源,但你的表单不是。如果您将路线弄平(使 cmets 成为自己的资源),那可能会起作用。或者,您可以尝试 bang bang 的方法并设置通过文章模型保存的嵌套属性,但我认为在这种情况下需要重新考虑您的 forn 结构
    • 我在脚手架创建它之后更改了该部分,因为当我在文章中嵌套 cmets 时,我收到提示错误(您的意思是@comment)最初是:(在 cmets 部分中)@ 987654324@ 然后我将其更改为:&lt;%= form_with @comment do |form| %&gt;
    • 或保持嵌套资源不变,只需将resources :comments添加到与resources :articles相同的级别,两种方式都可以。
    【解决方案2】:

    如果您尝试将 cmets 添加到文章中,我想您有一个嵌套资源,其中 Article 有许多 comments。现在你的/config/routes.rb 会有一个类似的条目:

    resources :articles do
      resources :comments
    end
    

    并假设您的/models.article.rb 模型中有accept_nested_attributes

    现在在您看来,您的表单通常应该类似于:

    <%= form_for [@article, @comment] do |f| %>
    

    在路由时,我发现使用 rails routes 命令很有用,但是一旦它们增长太多而不是将输出传送到 grep,我宁愿在开发中使用 gem sextant

    【讨论】:

    • 感谢您的帮助。实际上,正如您所料,我已经在路线中拥有资源:cmets 并且控制器具有操作。我在 rails 5 中没有 accept_nested_attributes (在指南中没有看到),我得到的默认值是:(在 cmets 的部分中,使用脚手架)&lt;%= **form_with** model: @comment do |form| %&gt; 请查看我更新的路线信息.rb & rails 路由 cmd.
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-28
    • 1970-01-01
    • 2019-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-24
    相关资源
    最近更新 更多