Rails中的内部消息传递 [英] Internal messaging in Rails
问题描述
我正在使用本教程来获取内部消息,这些消息在我的网站上起作用: http://www .novawave.net/public/rails_messaging_tutorial.html
I'm using this tutorial to get internal messages working on my site: http://www.novawave.net/public/rails_messaging_tutorial.html
但是,自从我最新升级到Rails 3以来,我遇到了以下错误:
But, since my latest upgrade to Rails 3, I'm getting this error:
NoMethodError in MsgController#sendmsg
undefined method `each' for #<String:0xcc8acc0>
应用程序跟踪:
app/models/message.rb:16:in `prepare_copies'
app/controllers/msg_controller.rb:140:in `sendmsg'
消息模型:
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
推荐答案
该教程似乎有些过时了.它使用Rails 2.0(可能还有一些同样古老的Ruby版本).
That tutorial seems a bit dated. It uses Rails 2.0 (and probably some equally old Ruby version).
您确定to
拥有一个数组(如您的attr_accessor
注释中所示)吗?错误消息似乎表明它是一个字符串.
Are you sure that to
holds an Array (as indicated in your attr_accessor
comment)? The error message seems to indicate that it is a String.
您以前是否已在Ruby 1.8(可能是Rails 2.3的版本)下运行此代码?
Did you previously have this code running under Ruby 1.8 (and, presumably, a version of Rails 2.3)?
在Ruby 1.8中,您可以将each
发送到String实例,并且(默认情况下)它将在字符串的行上进行迭代(实际上,它将在$/
上拆分并在结果上进行迭代).
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.9中,您需要使用each_line
遍历字符串的行.
In Ruby 1.9 you need to use each_line
to iterate over the lines of a string.
to.each_line do |recipient|
…
end
这篇关于Rails中的内部消息传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!