简体   繁体   English

Rails中的内部消息传递

[英]Internal messaging in Rails

I'm using this tutorial to get internal messages working on my site: http://www.novawave.net/public/rails_messaging_tutorial.html 我正在使用本教程来获取内部消息,这些消息在我的网站上有效: http : //www.novawave.net/public/rails_messaging_tutorial.html

But, since my latest upgrade to Rails 3, I'm getting this error: 但是,自从我最新升级到Rails 3以来,出现了以下错误:

NoMethodError in MsgController#sendmsg
undefined method `each' for #<String:0xcc8acc0>

Application trace: 应用程序跟踪:

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

The Message model: 消息模型:

class Message < ActiveRecord::Base
  belongs_to :author, :class_name => "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

That tutorial seems a bit dated. 该教程似乎有点过时了。 It uses Rails 2.0 (and probably some equally old Ruby version). 它使用Rails 2.0(可能还有一些同样古老的Ruby版本)。

Are you sure that to holds an Array (as indicated in your attr_accessor comment)? 您确定to保存一个数组(如attr_accessor注释中所示)吗? The error message seems to indicate that it is a String. 错误消息似乎表明它是一个字符串。

Did you previously have this code running under Ruby 1.8 (and, presumably, a version of Rails 2.3)? 您以前是否曾在Ruby 1.8(以及可能是Rails 2.3的版本)下运行此代码?

In Ruby 1.8 you could send each to String instances and it would (by default) iterate on the lines of the string (actually it would split on $/ and iterate on the result). 在Ruby 1.8中,您可以将each实例发送到String实例,并且(默认情况下)它将在字符串的行上进行迭代(实际上,它将在$/上拆分并在结果上进行迭代)。

In Ruby 1.9 you need to use each_line to iterate over the lines of a string. 在Ruby 1.9中,您需要使用each_line遍历字符串的各行。

to.each_line do |recipient|
  …
end

Seems you to is no anymore an Array but a String now. 看来您现在不再是Array,而是String。 You problem semmes becomes from your controller and how you define your to accessor inside it. 问题的根源来自于控制器,以及如何在其中定义访问器。

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

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