Rails 3 中的消息传递

发布于 2024-11-03 04:20:08 字数 2054 浏览 3 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

被你宠の有点坏 2024-11-10 04:20:08

您提到的网站上的正确代码片段是:

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

This is the 8th blackframe。

The proper code snippet at the site you have mentioned is:

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

This is the 8th black frame.

逐鹿 2024-11-10 04:20:08

在数组上调用每个函数的正确方法是使用第二个示例:

to.each do |recipient|  
  ...  
end

至于您收到的错误消息,似乎给定的 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:

to.each do |recipient|  
  ...  
end

As for the error message you receive, it appears that no Users exists for the given tos that you're looping over (User.find() returns nothing). The stack trace is complaining that you're calling the accessor id() 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 call id() and inbox() on nil records.

Another possibility is that you have an inbox that is nil, in which case you'll call id() again on a nil object.

躲猫猫 2024-11-10 04:20:08

几天前,我使用一个插件在我的应用程序上创建了一个消息系统,它运行得很好,完全支持 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文