首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Rails: NoMethodError in Articles#Show

Rails: NoMethodError in Articles#Show
EN

Stack Overflow用户
提问于 2017-01-12 11:09:17
回答 2查看 211关注 0票数 2

我正在编写started.html教程,使用Rails 5和Ruby2.4创建一个博客。在复制和粘贴完Unit-6:添加注释模型之后,Rails抛出了这个错误:

"NoMethodError in Articles#Show“:用于#<#的未定义方法`article_comments_path‘

代码语言:javascript
复制
<h2>Add a comment:</h2>
<%= form_for([@article, @article.comments.build]) do |f| %><!--****Error?****-->

2014年10月26日的堆栈溢出回答说,要向routes.rb添加一个routes.rb助手方法,如下所示:

代码语言:javascript
复制
resources :articles do
  resources :comments
end

但从那以后,语法似乎发生了一些变化。

我的routes.rb看起来是这样的:

代码语言:javascript
复制
Rails.application.routes.draw do
    resources :articles
    resources :comments#This creates comments as a nested resource within articles.
    root 'welcome#index'
    # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end

我没有在终端中发现任何拼写错误,所以我不太确定如何继续。如有任何指导,将不胜感激。

我的'rake路由\ grep注释‘输出是:

代码语言:javascript
复制
comments GET    /comments(.:format)          comments#index
         POST   /comments(.:format)          comments#create

new_comment GET /comments/新(.:format) comments#new edit_comment GET /评论/:id/编辑(.:format) comments#edit注释GET /.:format/:id(.:format) comments#show修补程序/:id(.:format) comments#update PUT /:.:format/ comments#update删除/注释/:id(.:format) comments#destroy

我的rake routes输出是:

代码语言:javascript
复制
     Prefix Verb   URI Pattern                  Controller#Action
    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
    comments GET    /comments(.:format)          comments#index
             POST   /comments(.:format)          comments#create
 new_comment GET    /comments/new(.:format)      comments#new
edit_comment GET    /comments/:id/edit(.:format) comments#edit
     comment GET    /comments/:id(.:format)      comments#show
             PATCH  /comments/:id(.:format)      comments#update
             PUT    /comments/:id(.:format)      comments#update
             DELETE /comments/:id(.:format)      comments#destroy
        root GET    /                            welcome#index

有什么问题吗?

EN

回答 2

Stack Overflow用户

发布于 2017-01-12 11:18:08

至少根据http://guides.rubyonrails.org/routing.html#nested-resources

你还需要做

代码语言:javascript
复制
resources :articles do
  resources :comments
end

设置此选项时,您会得到哪些错误?也尝试重新启动您的服务器。

票数 1
EN

Stack Overflow用户

发布于 2017-01-12 11:17:17

根据您提供的文档,您应该为嵌套路由使用一个块:

代码语言:javascript
复制
Rails.application.routes.draw do
resources :articles do
  resources :comments
end
root 'welcome#index'

Ruby不知道这种缩进,所以您的代码格式良好,如下所示:

代码语言:javascript
复制
Rails.application.routes.draw do
  resources :articles
  resources :comments#This creates comments as a nested resource within articles.
  root 'welcome#index
end

编辑:您可以始终使用rake routes检查cli中的路由。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41611649

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档