Ruby on Rails: NoMethodError в Articles#Show

Правя http://guides.rubyonrails.org/getting_started.html урока, създаване на блог с помощта на Rails 5 & Ruby 2.4. След като копирах и поставих моя път до края на Раздел-6: Добавяне на модел на коментари, Rails издаде тази грешка:

"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

Но синтаксисът изглежда се е променил малко оттогава.

Моят 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

Не открих никакви правописни грешки, въведени в терминала, така че не съм много сигурен как да продължа. Всякакви насоки са много ценени.

Моите „рейк маршрути | изходът на коментарите на grep е:

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

и моят 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

Някакви проблеми?


person Cody B. Coding    schedule 12.01.2017    source източник
comment
Просто проверете вашите маршрути за comments, като изпълните следното в терминала „рейк маршрути | grep коментари'   -  person Salil    schedule 12.01.2017
comment
Синтаксисът все още е същият за вложени маршрути. Какъв метод се опитва да извика?   -  person j-dexx    schedule 12.01.2017
comment
Моите рейк маршрути | grep comments output е: 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   -  person Cody B. Coding    schedule 12.01.2017


Отговори (2)


Поне според http://guides.rubyonrails.org/routing.html#nested-resources

Все още трябва да направите

resources :articles do
  resources :comments
end

Коя грешка получавате, когато зададете това? Също така опитайте да рестартирате сървъра си.

person Coolness    schedule 12.01.2017
comment
Получих същата грешка и проверявайки това, видях, че пропускам тази стъпка. Благодаря. - person nilsandrey; 07.03.2020

Според предоставените от вас документи трябва да използвате блок за вложени маршрути:

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

Редактиране: Винаги можете да проверите маршрутите си в cli с rake routes

person schnika    schedule 12.01.2017
comment
Моят отстъп в routes.rb е като вашия пример и моите рейк маршрути изглежда съответстват правилно. - person Cody B. Coding; 12.01.2017
comment
Добре. Просто направете каквото казват документите (вижте първия ми кодов блок) и сте готови :) - person schnika; 12.01.2017