【发布时间】:2017-01-12 11:09:17
【问题描述】:
我正在学习http://guides.rubyonrails.org/getting_started.html 教程,使用 Rails 5 和 Ruby 2.4 创建博客。在将我的方式复制并粘贴到第 6 单元:添加评论模型的末尾之后,Rails 抛出了这个错误:
“Articles#Show 中的 NoMethodError”:未定义的方法 #
<h2>Add a comment:</h2>
<%= form_for([@article, @article.comments.build]) do |f| %><!--****Error?****-->
2014 年 10 月 26 日的 Stackoverflow 回答说要向 routes.rb 添加 article_cmets_path 辅助方法,如下所示:
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 cmets 的输出是:
comments GET /comments(.:format) comments#index
POST /comments(.:format) comments#create
new_comment GET /cmets/new(.:format) cmets#new edit_comment GET /cmets/:id/edit(.:format) cmets#edit 评论 GET /cmets/:id(.:format) cmets#show PATCH /cmets/:id(.:format) cmets#update PUT /cmets/:id(.:format) cmets#update 删除 /cmets/:id(.:format) cmets#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
有什么问题吗?
【问题讨论】:
-
只需在终端 'rake routes | grep cmets'
-
nested routes 的语法仍然相同。它试图调用什么方法?
-
我的 rake 路线 | grep cmets 输出为: cmets GET /cmets(.:format) cmets#index POST /cmets(.:format) cmets#create new_comment GET /cmets/new(.:format) cmets#new edit_comment GET /cmets/:id/ edit(.:format) cmets#edit comment GET /cmets/:id(.:format) cmets#show PATCH /cmets/:id(.:format) cmets#update PUT /cmets/:id(.:format) cmets #update DELETE /cmets/:id(.:format) cmets#destroy
标签: ruby-on-rails ruby nested-attributes