Rails 3 中的消息传递
我正在使用 http://www.novawave.net/public/rails_messaging_tutorial.html 在我的 ruby on Rails 项目上实现消息传递的教程。我正在运行 ruby 1.9.2 和 Rails 3,并不断收到此错误
NoMethodError in SentController#create
undefined method 'each_line' for ["35"]:Array
应用程序跟踪:
app/models/message.rb:13:in 'prepare_copies'
app/controllers/sent_controller.rb:24:in 'create'
消息模型:
1 class Message < ActiveRecord::Base
2 belongs_to :author, :class_name => "User"
3 has_many :message_copies
4 has_many :recipients, :through => :message_copies
5 before_create :prepare_copies
6
7 attr_accessor :to #array of people to send to
8 attr_accessible :subject, :body, :to
9
10 def prepare_copies
11 return if to.blank?
12
13 to.each_line do |recipient|
14 recipient = User.find(recipient)
15 message_copies.build(:recipient_id => recipient.id, :folder_id => recipient.inbox.id)
16 end
17 end
18 end
发送控制器:
class SentController < ApplicationController
...
def create
current_user = User.find(session[:user_id])
@message = current_user.sent_messages.build(params[:message])
if @message.save
flash[:notice] = "Message sent."
redirect_to :action => "index"
else
render :action => "new"
end
end
end
如果我编辑和使用:
to.each do |recipient|
...
end
我收到不同的错误:
RuntimeError in SentController#create
Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id
应用程序跟踪:
app/models/message.rb:15:in 'block in prepare_copies'
app/models/message.rb:13:in 'each'
app/models/message.rb:13:in 'prepare_copies'
app/controllers/sent_controller.rb:24:in 'create'
I'm using the http://www.novawave.net/public/rails_messaging_tutorial.html tutorial to implement messaging on my ruby on rails project. I am running ruby 1.9.2 and rails 3 and keep getting this error
NoMethodError in SentController#create
undefined method 'each_line' for ["35"]:Array
Application Trace:
app/models/message.rb:13:in 'prepare_copies'
app/controllers/sent_controller.rb:24:in 'create'
Message model:
1 class Message < ActiveRecord::Base
2 belongs_to :author, :class_name => "User"
3 has_many :message_copies
4 has_many :recipients, :through => :message_copies
5 before_create :prepare_copies
6
7 attr_accessor :to #array of people to send to
8 attr_accessible :subject, :body, :to
9
10 def prepare_copies
11 return if to.blank?
12
13 to.each_line do |recipient|
14 recipient = User.find(recipient)
15 message_copies.build(:recipient_id => recipient.id, :folder_id => recipient.inbox.id)
16 end
17 end
18 end
Sent controller:
class SentController < ApplicationController
...
def create
current_user = User.find(session[:user_id])
@message = current_user.sent_messages.build(params[:message])
if @message.save
flash[:notice] = "Message sent."
redirect_to :action => "index"
else
render :action => "new"
end
end
end
If I edit and use:
to.each do |recipient|
...
end
I get a different error:
RuntimeError in SentController#create
Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id
Application Trace:
app/models/message.rb:15:in 'block in prepare_copies'
app/models/message.rb:13:in 'each'
app/models/message.rb:13:in 'prepare_copies'
app/controllers/sent_controller.rb:24:in 'create'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您提到的网站上的正确代码片段是:
This is the 8th blackframe。
The proper code snippet at the site you have mentioned is:
This is the 8th black frame.
在数组上调用每个函数的正确方法是使用第二个示例:
至于您收到的错误消息,似乎给定的
to
不存在User
您正在循环(User.find()
不返回任何内容)。堆栈跟踪抱怨您在给定以下行的 nil 对象上调用访问器id()
::recipient_id => recipient.id
创建一个模拟
User
并仅向该用户发送一条消息,以验证这是否是导致错误的原因。如果是这样,您将需要一些错误处理来确保您不会对 nil 记录调用id()
和inbox()
。另一种可能性是您的收件箱为 nil,在这种情况下,您将在 nil 对象上再次调用 id() 。
The correct way to call each on an Array is with your second example:
As for the error message you receive, it appears that no
User
s exists for the givento
s that you're looping over (User.find()
returns nothing). The stack trace is complaining that you're calling the accessorid()
on a nil object given this line::recipient_id => recipient.id
Create a mock
User
and send a message to only that User to verify that this is the cause of your error. If it is you'll need some error handling to ensure that you don't callid()
andinbox()
on nil records.Another possibility is that you have an
inbox
that is nil, in which case you'll callid()
again on a nil object.几天前,我使用一个插件在我的应用程序上创建了一个消息系统,它运行得很好,完全支持 Rails 3。如果您仍然遇到问题,请尝试 它出来。
I used a plugin to create a messaging system on my app days ago and it worked pretty well, with full support to Rails 3. If you still with problems, try it out.