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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24331892/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 23:44:12  来源:igfitidea点击:

LoadError Unable to autoload constant Message

ruby-on-railsactivemodelrails-modelsmailboxer

提问by pwz2000

In my app; when I submit form, I get this error:

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

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

It points to the createaction in the Questionscontroller:

它指向控制器中的create动作Questions

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

Message model:

消息模型:

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

回答by kik

By convention in rails (and this is enforced by autoloader), file paths should match namespaces.

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

So, if you have a Mailboxer::Messagemodel, it should be in app/models/mailboxer/message.rb.

所以,如果你有一个Mailboxer::Message模型,它应该在app/models/mailboxer/message.rb.

Additionally, you probably have autoloader kicking in when trying to load a Messageclass (my guess is that it happens from within ActAsMessageable). It looks for a message.rbfile in load path, find it in app/model/and thus load that file so it can find the Messageclass.

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

Problem is, it doesn't find a Messageclass in that file, only a Mailboxer::Messageclass (which is radically different). This is why it throws "Unable to autoload constant Message, expected /app/models/message.rb to define it".

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

To fix that, create directory app/models/mailboxer/and put Mailboxer::Messagein it.

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

回答by Jay

I got this during integration testing. Turns out, it was fixtures related. Had to delete the my unused file in /test/fixtures/wrong_name.yml

我在集成测试期间得到了这个。原来,这与固定装置有关。不得不删除 /test/fixtures/wrong_name.yml 中我未使用的文件

回答by tebayoso

As stated in the documentation, to send a message from Amodel to Bmodel you have to add:

如文档中所述,要在A模型之间发送消息,B您必须添加:

acts_as_messageable 

in both models.

在这两种模型中。

And then do:

然后做:

a.send_message(b, "Body", "subject")

So in your models:

所以在你的模型中:

  class User < ...
    act_as_messageable
  end

@question_sendermust be a Userinstance.

@question_sender必须是一个User实例。

@question_sender.send_message({attr_accessor_hash}, recipient_user, @question.body, "You have a question from #{@question_sender.id}")

As long as the attr_accessoris not related to the gem, and the method send_message is not aware of this attributes you will have to redefine it:

只要attr_accessor与 gem 无关,并且方法 send_message 不知道此属性,您将不得不重新定义它:

https://github.com/mailboxer/mailboxer/blob/master/lib/mailboxer/models/messageable.rb#L60

https://github.com/mailboxer/mailboxer/blob/master/lib/mailboxer/models/messageable.rb#L60

add the attr_accessor_hash to method

将 attr_accessor_hash 添加到方法中

def send_message({attr_accesor_hash}, recipients, msg_body, subject, sanitize_text=true, attachment=nil, message_timestamp = Time.now)

And look at the code add the fields where you need as: attr_accessor["param"]

并查看代码在您需要的位置添加字段: attr_accessor["param"]

回答by Acacia

Notice these lines;

注意这些行;

@question = Question.new(params[:question])

@question.message = @message

and ;

和 ;

attr_accessible :answer, :question, :sender_id, :recipient_id, :conversation_id

The @question.messageline is calling an attribute that is not accessible in the Question Modelso do this;

@question.message行正在调用一个无法访问的属性,Question Model所以这样做;

attr_accessible :answer, :question, :sender_id, :recipient_id, :conversation_id, message