2010-10-10 40 views
0

我使用本教程中获得内部消息在我的网站的工作:http://www.novawave.net/public/rails_messaging_tutorial.html在Rails的内部消息

但是,因为我最近升级到Rails 3中,我得到这个错误:

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 
+0

我们需要你的控制器msg_controller,因为它的错误在于。 – shingara 2010-10-10 09:45:28

回答

1

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

您确定to拥有阵列(如您的attr_accessor评论所示)?错误消息似乎表明它是一个字符串。

您是否曾经在Ruby 1.8下运行过这段代码(推测是Rails 2.3的一个版本)?

在Ruby 1.8,你可以发送each为String的实例,它会(默认)对字符串的行迭代(实际上它会各执$/和迭代的结果)。

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

to.each_line do |recipient| 
    … 
end 
0

似乎你已经不再是一个数组,而是一个字符串了。您的问题从您的控制器变为现实,您如何在其中定义您的存取器。