【问题标题】:Error when implement acts_as_commentable: uninitialized constant Discussion::Comment实施acts_as_commentable时出错:未初始化的常量 Discussion::Comment
【发布时间】:2019-02-26 04:36:03
【问题描述】:

尝试为我的讨论模型实现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

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

<% 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

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

 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

【问题讨论】:

  • 能否请您添加完整的错误堆栈跟踪?哪一行代码指出了错误?
  • 我在我的视图中添加了指向代码行的错误图像。我不确定如何提取完整的错误堆栈跟踪。是服务器日志中的代码吗?
  • 能否添加由 gem 生成的迁移代码?您是开始使用acts_as_commentable_with_threading gem 还是从acts_as_commentable 升级的?
  • 我使用了acts_as_commentable_with_threading迁移。迁移代码直接来自 gem 自述文件,在 Migration 类行的末尾添加了 [5.1]。
  • 查看添加到问题中的 cmets 模型架构。

标签: ruby-on-rails rubygems acts-as-commentable


【解决方案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 的所有帮助!

【讨论】:

  • 很高兴我的 cmets 帮助了你!
猜你喜欢
  • 1970-01-01
  • 2023-03-20
  • 2017-03-13
  • 1970-01-01
  • 1970-01-01
  • 2014-01-08
  • 1970-01-01
  • 1970-01-01
  • 2011-09-29
相关资源
最近更新 更多