Getting Started With Rails Guide (Step 6) - Routing Error

Hello,

I’ve been trying to solve this issue for a few hours and couldn’t find
similar issues by googling. I am very new to Rails. After completing all
the steps of “6 Adding a Second Model”, I run into the following error
at
localhost:3000/articles/1/comments

Routing Error
undefined local variable or method ` ’ for CommentsController:Class

app/controllers/comments_controller.rb:7:in `class:CommentsController
http://localhost:3000/articles/5/comments#

app/controllers/comments_controller.rb:1:in `<top (required)>’
http://localhost:3000/articles/5/comments#

The error occurs after clicking “Create Comment”. I triple checked the
guide to ensure I copy and pasted everything properly and followed every
step but I still cannot figure it out. Since this appears to be a
routing issue I pasted all code relevant to routing below. Hopefully
I’ve provided enough information. Thank you.

routes.rb

Rails.application.routes.draw do

resources :articles do
resources :comments
end

get ‘articles/index’
get ‘welcome/index’
root ‘welcome#index’

end

show.html.erb

Title: <%= @article.title %>

Text: <%= @article.text %>

Comments

<% @article.comments.each do |comment| %>

Commenter: <%= comment.commenter %>

Comment: <%= comment.body %>

<% end %>

Add a comment:

<%= form_for([@article, @article.comments.build]) do |f| %>

<%= f.label :commenter %>
<%= f.text_field :commenter %>

<%= f.label :body %>
<%= f.text_area :body %>

<%= f.submit %>

<% end %>

<%= link_to ‘Edit’, edit_article_path(@article) %> |
<%= link_to ‘Back’, articles_path %>

articles_controller.rb

class ArticlesController < ApplicationController

    def index
            @articles = Article.all
    end

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

    def new
            @article = Article.new
    end

    def edit
            @article = Article.find(params[:id])
    end

    def create
            @article = Article.new(article_params)

            if @article.save
               redirect_to @article
            else
               render 'new'
            end
    end

    def update
               @article = Article.find(params[:id])

            if @article.update(article_params)
               redirect_to @article
            else
       render 'edit'
            end
    end

    def destroy
            @article = Article.find(params[:id])
            @article.destroy

            redirect_to articles_path
    end

    private
    def article_params
    params.require(:article).permit(:title, :text)
    end

end

comments_controller.rb

class CommentsController < ApplicationController
def create
@article = Article.find(params[:article_id])
@comment = @article.comments.create(comment_params)
redirect_to article_path(@article)
end

    private
      def comment_params
          params.require(:comment).permit(:commenter, :body)
      end

end

article.rb

class Article < ActiveRecord::Base
has_many :comments
validates :title, presence: true,
length: {minimum: 5}
end

comment.rb

class Comment < ActiveRecord::Base
belongs_to :article
end

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs