I'm doing the http://guides.rubyonrails.org/getting_started.html tutorial, creating a blog using Rails 5 & Ruby 2.4. After copying and pasting my way through to the end of Unit-6: Adding a Comments model, Rails threw this error:
"NoMethodError in Articles#Show":undefined method `article_comments_path' for #<#
<h2>Add a comment:</h2>
<%= form_for([@article, @article.comments.build]) do |f| %><!--****Error?****-->
A Stackoverflow answer from Oct. 26, 2014 says to add an article_comments_path helper method to routes.rb like this:
resources :articles do
resources :comments
end
But the syntax seems to have changed a bit since.
My routes.rb looks like this:
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
I haven't found any spelling errors entered in the terminal, so I'm not really sure on how to proceed. Any guidance is greatly appreciated.
My 'rake routes | grep comments' output is:
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
and my rake routes
output is:
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
Any problems?
According to the docs you provided you should use a block for nested routes:
Rails.application.routes.draw do
resources :articles do
resources :comments
end
root 'welcome#index'
Ruby is not aware of this indentation so your code well formatted looks like this:
Rails.application.routes.draw do
resources :articles
resources :comments#This creates comments as a nested resource within articles.
root 'welcome#index
end
Edit: You can always check your routs within the cli with rake routes
Atleast according to http://guides.rubyonrails.org/routing.html#nested-resources
You still need to do
resources :articles do
resources :comments
end
Which error do you get when you set this? Also try restarting your server.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.