Error when implement acts_as_commentable: uninitialized constant Discussion::Comment

88 Views Asked by At

I get the following server log error when trying to implement the acts_as_commentable gem for my Discussion model: NameError - uninitialized constant Discussion::Comment:

I have generated the migration file from the readme and ran rails db:migrate.

 rails generate acts_as_commentable_with_threading_migration

I have tried restarting the app.

I have followed the usage instructions on the read me to add this to my model file:

class Discussion < ApplicationRecord
  acts_as_commentable
end

Summary of view code to try to display list of comments for the Discussion model:

<% Discussion.where(guide_id: @guide.id).order(:created_at).each do|discussion| %>
 <% discussion.comment_threads.each do |comment| %>
   <p><%= comment.body %></p>
 <% end %>
<% end %>

image of error pointing to the view line of code

The schema.rb file includes the comments model added through the migration in the gem readme:

 create_table "comments", force: :cascade do |t|
    t.integer "commentable_id"
    t.string "commentable_type"
    t.string "title"
    t.text "body"
    t.string "subject"
    t.integer "user_id", null: false
    t.integer "parent_id"
    t.integer "lft"
    t.integer "rgt"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["commentable_id", "commentable_type"], name: "index_comments_on_commentable_id_and_commentable_type"
    t.index ["user_id"], name: "index_comments_on_user_id"
  end
1

There are 1 best solutions below

1
eddie paulino On

It appears that the issue was that there was no comment.rb file created when I ran the migration. I may have ran the migration prior to deleting the old model file.

rails generate acts_as_commentable_with_threading_migration

I had a comment model I had previously created prior to trying to using the acts_as_commentable gem. I tried deleting the migration, routes, controller, model, and view files and then using rails db:drop followed by rails db:create and rails db:migrate to start from scratch.

After restarting the server, the comment gem is now working.

Thanks for all the help @Vasilisa!