简体   繁体   English

LoadError无法自动加载常量消息

[英]LoadError Unable to autoload constant Message

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 create action in the Questions controller: 它指向Questions控制器中的create动作:

@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 convention in rails (and this is enforced by autoloader), file paths should match namespaces. 按惯例,在rails中(这是由自动加载器强制执行的),文件路径应该与命名空间匹配。

So, if you have a Mailboxer::Message model, 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 Message class (my guess is that it happens from within ActAsMessageable). 此外,您可能在尝试加载Message类时启动了自动加载器(我的猜测是它发生在ActAsMessageable中)。 It looks for a message.rb file in load path, find it in app/model/ and thus load that file so it can find the Message class. 它在加载路径中查找message.rb文件,在app/model/找到它,然后加载该文件,以便它可以找到Message类。

Problem is, it doesn't find a Message class in that file, only a Mailboxer::Message class (which is radically different). 问题是,它在该文件中找不到Message类,只找到一个Mailboxer::Message类(它完全不同)。 This is why it throws "Unable to autoload constant Message, expected /app/models/message.rb to define it". 这就是它抛出“无法自动加载常量消息,预期/app/models/message.rb来定义它”的原因。

To fix that, create directory app/models/mailboxer/ and put Mailboxer::Message in it. 要解决此问题,请创建目录app/models/mailboxer/并将Mailboxer::Message放入其中。

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中我未使用的文件

As stated in the documentation, to send a message from A model to B model 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_sender must be a User instance. @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_accessor is 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"]

Notice these lines; 注意这些线;

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

@question.message = @message

and ; 和;

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

The @question.message line is calling an attribute that is not accessible in the Question Model so do this; @question.message行调用了一个在Question Model无法访问的属性,所以这样做;

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM