我试图用Mailboxer向用户发送通知,并且需要传递一个对象,该对象将影响通知在我的navbar的通知下拉列表中的显示方式。
@recipient.notify("#{current_user.name} needs you to review his help with: #{@offer.title}", "#{@message}", @offer)最后一个参数是我试图传递对象的位置,@offer。
这是我试图使用的Mailboxer方法:
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通知的方式。我很难找到他们的信息。任何帮助都是非常感谢的。
发布于 2014-09-21 18:33:57
尝试以下方法以获取未读通知
current_user.mailbox.notifications(:read => false)发布于 2015-12-03 13:51:23
如https://github.com/mailboxer/mailboxer/blob/master/app/models/mailboxer/notification.rb所示
Notification.rb为已通知对象提供了多态关联。
belongs_to :notified_object, :polymorphic => :true因此,需要将通知指定为要传递的对象模型中的关联。在这里你可以在Offer.rb找到前男友
has_many :notifications , as: :notified_object.https://stackoverflow.com/questions/25706280
复制相似问题