Cameron Bass Cameron Bass - 5 months ago 8
Ruby Question

uninitialized constant CommentsController::Comments - Rails error

I have just made a new model named

Comments
and now I'm trying to make a view so that user can leave comments from the UI(very basic stuff here). But, I'm running into this error
uninitialized constant CommentsController::Comments
for the life of me I can't figure out why it's throwing this error and not just rendering the page when it's clicked?
I'm going to post all the applicable code and error for clarity.

MODEL:

class Comment < ActiveRecord::Base
belongs_to :subscriber
end


CONTROLLER:

class CommentsController < ApplicationController
def new
@comments = Comments.new
end
end


ROUTES

Rails.application.routes.draw do
devise_for :users
resources :subscribers, except: :show
resources :comments


SCHEMA:

create_table "comments", force: :cascade do |t|
t.string "description"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "fav_drink"
t.string "visit_time"
end


VIEW:

<h2>Let us get to know you more <%= link_to 'Comment', new_comment_path %> </h2>


This is the link I've set up to click the the page where the user will leave a comment.

ERROR:

enter image description here

Let me know if you need to see anymore code. Thank you!

Answer

Models are referenced singularly as they are classes. You have made a mistake of Comments.new but it should be Comment.new.

Comments