2010-10-10 53 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

似乎你已經不再是一個數組,而是一個字符串了。您的問題從您的控制器變爲現實,您如何在其中定義您的存取器。