简体   繁体   English

遵循RoR指南的第6.4步创建博客后Articles#show中的NoMethodError

[英]NoMethodError in Articles#show after following step 6.4 of RoR Guide Creating a Blog

I'm new to RoR and I'm trying to follow the guide at http://guides.rubyonrails.org/getting_started.html I'm currently at section 6 point 4 but I'm getting stuck here. 我是RoR的新手,我正尝试按照http://guides.rubyonrails.org/getting_started.html上的指南进行操作。我目前位于第6节第4点,但我一直被困在这里。

I'm getting the following error: NoMethodError in Articles#show 我收到以下错误:Articles#show中的NoMethodError

Showing /Users/riaanvanstraaten/rubyapps/blog/app/views/articles/show.html.erb where line #12 raised:

undefined method `article_comments_path' for #<#<Class:0x007fac016d6d20>:0x007fac01c05f30>
Extracted source (around line #12):
10                   
11 <h2>Add a comment:</h2>
12 <%= form_for([@article, @article.comments.build]) do |f| %>
13  <p>
14    <%= f.label :commenter %><br>
15    <%= f.text_field :commenter %>

I've double checked the code from the guide but I cannot see where I am going wrong. 我已经仔细检查了指南中的代码,但是看不到哪里出了问题。

I've searched for some advise and followed some suggestions with no positive result. 我一直在寻找一些建议,并遵循了一些建议,但都没有取得积极的结果。 I've one post that is very close to my problem, but the mentioned solutions does not resolve my current problem. 我有一篇帖子非常贴近我的问题,但是上述解决方案无法解决我当前的问题。 This is the link to the similiar post: NoMethodError in Articles#show.undefined method `article_comments_path' 这是指向类似文章的链接: Articles#show.undefined方法“ article_comments_path”中的NoMethodError

Here is the code from the files used 这是使用的文件中的代码

show.html.erb show.html.erb

<p>
  <strong>Title:</strong>
  <%= @article.title %>
</p>

<p>
  <strong>Text:</strong>
  <%= @article.text %>
</p>

<h2>Add a comment:</h2>
<%= form_for([@article, @article.comments.build]) do |f| %>
  <p>
    <%= f.label :commenter %><br>
    <%= f.text_field :commenter %>
  </p>
  <p>
    <%= f.label :body %><br>
    <%= f.text_area :body %>
  </p>
  <p>
    <%= f.submit %>
  </p>
<% end %>

<%= link_to 'Back', articles_path %> |
<%= link_to 'Edit', edit_article_path(@article) %>

comments_controller.html.erb comments_controller.html.erb

class CommentsController < ApplicationController
  def create
    @article = Article.find(params[:article_id])
    @comment = @article.comments.create(comment_params)
    redirect_to article_path(@article)
  end

  private
    def comment_params
      params.require(:comment).permit(:commenter, :body)
    end
end

routes.rb 的routes.rb

Rails.application.routes.draw do

    resources :articles do
        resources :commments
    end
end

Here is my 'rake routes' output 这是我的“耙路”输出

               Prefix Verb   URI Pattern                                        Controller#Action
    article_commments GET    /articles/:article_id/commments(.:format)          commments#index
                      POST   /articles/:article_id/commments(.:format)          commments#create
 new_article_commment GET    /articles/:article_id/commments/new(.:format)      commments#new
edit_article_commment GET    /articles/:article_id/commments/:id/edit(.:format) commments#edit
     article_commment GET    /articles/:article_id/commments/:id(.:format)      commments#show
                      PATCH  /articles/:article_id/commments/:id(.:format)      commments#update
                      PUT    /articles/:article_id/commments/:id(.:format)      commments#update
                      DELETE /articles/:article_id/commments/:id(.:format)      commments#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

What's wrong? 怎么了? I copied and pasted the code from the website to see what I'm doing wrong, but I still cannot to resolve the issue. 我从网站复制并粘贴了代码,以查看我做错了什么,但仍然无法解决问题。

Thanks in advance for your help! 在此先感谢您的帮助!

There is a typo in your routes.rb . 您的routes.rb有一个错字。 You have 3 m letters in word comments instead of 2 here: 您的单词注释中有3 m字母,而不是2个:

Rails.application.routes.draw do
  resources :articles do
    resources :commments
  end
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM