文章#中的NoMethodError在遵循RoR指南创建博客的步骤6.4之后显示

时间:2015-01-14 19:53:57

标签: ruby-on-rails ruby

我是RoR的新手,我正试图按照http://guides.rubyonrails.org/getting_started.html的指南进行操作 我现在在第6点第4点,但我被困在这里。

我收到以下错误:文章中的NoMethodError#show

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 %>

我已经仔细检查了指南中的代码,但我看不出我出错的地方。

我已经搜索了一些建议并遵循了一些没有积极结果的建议。 我有一篇文章非常接近我的问题,但提到的解决方案并没有解决我当前的问题。这是类似帖子的链接:NoMethodError in Articles#show.undefined method `article_comments_path'

以下是使用文件的代码

  

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

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

Rails.application.routes.draw do

    resources :articles do
        resources :commments
    end
end

这是我的'rake routes'输出

               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

怎么了?我复制并粘贴了网站上的代码,看看我做错了什么,但我还是无法解决这个问题。

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:2)

routes.rb中有拼写错误。您在单词注释中有3个m个字母而不是2个:

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