Rails中的内部消息传递

我正在使用本教程来获取在我的网站上运行的内部消息: http : //www.novawave.net/public/rails_messaging_tutorial.html

但是,自从我最近升级到Rails 3后,我收到了这个错误:

NoMethodError in MsgController#sendmsg undefined method `each' for # 

应用跟踪:

 app/models/message.rb:16:in `prepare_copies' app/controllers/msg_controller.rb:140:in `sendmsg' 

消息模型:

 class Message  "User" has_many :message_copies has_many :recipients, :through => :message_copies before_create :prepare_copies attr_accessor :to # array of people to send to attr_accessible :subject, :body, :to def prepare_copies return if to.blank? to.each do |recipient| recipient = User.find(recipient) message_copies.build(:recipient_id => recipient.id, :folder_id => recipient.inbox.id) end end end 

该教程似乎有点过时了。 它使用Rails 2.0(可能还有一些同样古老的Ruby版本)。

你确定to保存一个数组(如attr_accessor注释中所示)吗? 错误消息似乎表明它是一个String。

您以前是否在Ruby 1.8下运行此代码(可能是Rails 2.3的一个版本)?

在Ruby 1.8中,您可以将each实例发送到String实例,并且它(默认情况下)将迭代字符串的行(实际上它将拆分为$/并迭代结果)。

在Ruby 1.9中,您需要使用each_line来迭代字符串的行。

 to.each_line do |recipient| … end 

似乎你现在不再是一个数组而是一个字符串。 您的问题semmes来自您的控制器以及您如何在其中定义您的访问者。