NoMethodError in Articles # show.undefined method `article_comments_path '

    **ARTICLE CONTROLLER**                  //controller
class ArticlesController < ApplicationController
	def index
	@article=Article.all
	end
	def show
	@article=Article.find(params[:id])	
	@comment = Comment.new
    @comment.article_id = @article.id 	
	end
	def new
	@article=Article.new
	end
	def create
	@article = Article.new(article_params)
    @article.save
    redirect_to article_path(@article)
	end
	def destroy
	article=Article.find(params[:id])
    article.destroy
    redirect_to articles_path
	end
	def edit
	 @article = Article.find(params[:id])	
	end
	def update
	@article = Article.find(params[:id])
    @article.update(article_params)
    flash.notice = "Article '#{@article.title}' Updated!"
    redirect_to article_path(@article)
	end
	def article_params
    params.require(:article).permit(:title, :body)
    end
end

**SHOW.HTML.ERB**            /view file

<h1><%=@article.title%></h1>
<p><%=@article.body%></p>
<br><hr>

<%= link_to "edit", edit_article_path(@article) %>|
<%= link_to "delete",article_path(@article), method: :delete,data: {confirm: "Really delete the article?"} %>|
<%= link_to "<< Back to Articles List", articles_path %>
<h3>Comments</h3>
<%= render partial: 'articles/comment', collection: @article.comments %>
<%= render partial: 'comments/form' %>

**_FORM.HTML.ERB**             //_form view
<h3>Post a Comment</h3>

<%= form_for [ @article, @comment ] do |f| %>
  <p>
    <%= f.label :author_name %><br/>
    <%= f.text_field :author_name %>
  </p>
  <p>
    <%= f.label :body %><br/>
    <%= f.text_area :body %>
  </p>
  <p>
    <%= f.submit 'Submit' %>
  </p>
<% end %>
   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
    root GET    /                            articles#index
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
      

Run codeHide result


I have no articles_comments_path! NoMethodError in Articles # show Displaying / home / manoj / ror / blogger / app / views / comments / _form.html.erb where line # 3 is raised:

undefined method article_comments_path' for #<#<Class:0x00000005be3d40>:0x000000054cacc0> app/views/comments/_form.html.erb:3:in

_app_views_comments__form_html_erb__3077497298558231225_47236640 'app / views / articles / show.html.erb: 11: in `_app_views_articles_show_html_erb__4529829459036724249_48053660'

+3


source to share


1 answer


You are getting this error because you don't have the article_comments_path helper method. Modify the following routes:

resources :articles do
  resources :comments
end

      

OR

If you don't want to insert routes, change the form to this:



<%= form_for @comment do |f| %>
  // form fields
<% end %>

      

Also in the show activity you can dry out your code using association methods

def show
  @article= Article.find(params[:id])   
  @comment = @article.comments.build 
end

      

+7


source







All Articles