使用Mailboxer通知传递对象

时间:2014-09-07 01:24:00

标签: ruby-on-rails notifications mailboxer

我正在尝试向使用Mailboxer的用户发送通知,我需要传递一个对象,该对象将影响通知在导航栏的通知下拉列表中的显示方式。

@recipient.notify("#{current_user.name} needs you to review his help with: #{@offer.title}", "#{@message}", @offer)

最后一个参数是我试图传递对象的地方@offer

这是我正在尝试使用的邮箱方法:

 def notify(subject,body,obj = nil,sanitize_text=true,notification_code=nil,send_mail=true)
    Mailboxer::Notification.notify_all([self],subject,body,obj,sanitize_text,notification_code,send_mail)
  end

它调用此notify_all方法:

def notify_all(recipients, subject, body, obj = nil, sanitize_text = true, notification_code=nil, send_mail=true)
  notification = Mailboxer::NotificationBuilder.new({
    :recipients        => recipients,
    :subject           => subject,
    :body              => body,
    :notified_object   => obj,
    :notification_code => notification_code
  }).build

  notification.deliver sanitize_text, send_mail
end

当我尝试使用<%= notification.object_id %>访问对象时,我得到一个像205981093这样的长数字。如果我尝试使用以下代码访问其中一个offer对象的字段,则会收到错误:<%= notification.object_id.title %>

`undefined method `title' for 2166878920:Fixnum`

我甚至不确定这是否是使用Mailboxer Notifications的方式。我很难找到他们的信息。非常感谢任何帮助。

2 个答案:

答案 0 :(得分:3)

https://github.com/mailboxer/mailboxer/blob/master/app/models/mailboxer/notification.rb

所示

Notification.rb为通知对象提供多态关联。

belongs_to :notified_object, :polymorphic => :true

因此,您需要将通知指定为要传递的对象模型中的关联。 对于ex,你可以在Offer.rb

has_many :notifications , as: :notified_object.

答案 1 :(得分:2)

尝试以下操作以获取未读通知

current_user.mailbox.notifications(:read => false)