0

尝试为我的讨论模型实现acts_as_commentable gem 时出现以下服务器日志错误: NameError - uninitialized constant Discussion::Comment:

我已经从自述文件中生成了迁移文件并运行了 rails db:migrate。

 rails generate acts_as_commentable_with_threading_migration

我已经尝试重新启动应用程序。

我已按照阅读我的使用说明将其添加到我的模型文件中:

class Discussion < ApplicationRecord
  acts_as_commentable
end

尝试显示 Discussion 模型的评论列表的视图代码摘要:

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

指向代码视图行的错误图像

schema.rb 文件包括通过 gem 自述文件中的迁移添加的注释模型:

 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
4

1 回答 1

1

看来问题是我运行迁移时没有创建 comment.rb 文件。我可能在删除旧模型文件之前进行了迁移。

rails generate acts_as_commentable_with_threading_migration

在尝试使用acts_as_commentable gem 之前,我有一个之前创建的评论模型。我尝试删除迁移、路由、控制器、模型和视图文件,然后使用 rails db:drop 后跟 rails db:create 和 rails db:migrate 从头开始​​。

重新启动服务器后,评论 gem 现在正在工作。

感谢@Vasilisa 的所有帮助!

于 2019-02-27T13:50:28.377 回答