我正在编写started.html教程,使用Rails 5和Ruby2.4创建一个博客。在复制和粘贴完Unit-6:添加注释模型之后,Rails抛出了这个错误:
"NoMethodError in Articles#Show“:用于#<#的未定义方法`article_comments_path‘
<h2>Add a comment:</h2>
<%= form_for([@article, @article.comments.build]) do |f| %><!--****Error?****-->2014年10月26日的堆栈溢出回答说,要向routes.rb添加一个routes.rb助手方法,如下所示:
resources :articles do
resources :comments
end但从那以后,语法似乎发生了一些变化。
我的routes.rb看起来是这样的:
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注释‘输出是:
comments GET /comments(.:format) comments#index
POST /comments(.:format) comments#createnew_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输出是:
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有什么问题吗?
发布于 2017-01-12 11:18:08
至少根据http://guides.rubyonrails.org/routing.html#nested-resources
你还需要做
resources :articles do
resources :comments
end设置此选项时,您会得到哪些错误?也尝试重新启动您的服务器。
发布于 2017-01-12 11:17:17
根据您提供的文档,您应该为嵌套路由使用一个块:
Rails.application.routes.draw do
resources :articles do
resources :comments
end
root 'welcome#index'Ruby不知道这种缩进,所以您的代码格式良好,如下所示:
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中的路由。
https://stackoverflow.com/questions/41611649
复制相似问题