0

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

this: でオブジェクトにアクセスしようとすると<%= notification.object_id %>、 のような長い数値が返され205981093ます。offerこれでオブジェクトのフィールドの1 つにアクセスしようとすると、エラーが発生します。<%= notification.object_id.title %>

`undefined method `title' for 2166878920:Fixnum`

これが Mailboxer Notifications の使用方法であるかどうかさえわかりません。それらに関する情報を見つけるのに非常に苦労しました。どんな助けでも大歓迎です。

4

2 に答える 2

3

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.
于 2015-12-03T13:51:23.717 に答える
2

未読通知を取得するには、次のことを試してください

current_user.mailbox.notifications(:read => false)
于 2014-09-21T18:33:57.430 に答える