gpt4 book ai didi

ruby-on-rails - LoadError 无法自动加载常量消息

转载 作者:行者123 更新时间:2023-12-03 05:59:14 27 4
gpt4 key购买 nike

在我的应用程序中;当我提交表单时,出现此错误:

LoadError at /questions
Unable to autoload constant Message, expected /app/models/message.rb to define it

它指向 Questions Controller 中的 create 操作:

@message = current_user.messages.new(:subject => "You have a question from #{@question.sender_id}"`

消息模型:

class Mailboxer::Message < ActiveRecord::Base
attr_accessible :notification_id, :receiver_id, :conversation_id
end

最佳答案

按照 Rails 中的约定(这是由自动加载器强制执行的),文件路径应与命名空间匹配。

因此,如果您有 Mailboxer::Message 模型,它应该位于 app/models/mailboxer/message.rb 中。

此外,当尝试加载 Message 类时,您可能会启动自动加载器(我的猜测是它发生在 ActAsMessageable 内部)。它在加载路径中查找 message.rb 文件,在 app/model/ 中找到它,然后加载该文件,以便找到 Message 类。

问题是,它在该文件中找不到 Message 类,只有 Mailboxer::Message 类(这是不同的)。这就是为什么它抛出“无法自动加载常量消息,需要/app/models/message.rb 来定义它”。

要解决此问题,请创建目录 app/models/mailboxer/ 并将 Mailboxer::Message 放入其中。

关于ruby-on-rails - LoadError 无法自动加载常量消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24331892/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com