问题
I'm using this tutorial to get internal messages working on my site: http://www.novawave.net/public/rails_messaging_tutorial.html
But, since my latest upgrade to Rails 3, I'm getting this error:
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
回答1:
That tutorial seems a bit dated. It uses Rails 2.0 (and probably some equally old Ruby version).
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.
Did you previously have this code running under Ruby 1.8 (and, presumably, a version of 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).
In Ruby 1.9 you need to use each_line
to iterate over the lines of a string.
to.each_line do |recipient|
…
end
回答2:
Seems you to is no anymore an Array but a String now. You problem semmes becomes from your controller and how you define your to accessor inside it.
来源:https://stackoverflow.com/questions/3899789/internal-messaging-in-rails