我已设置公共活动以显示通知。使用正确的收件人创建通知。
现在我想向通知收件人发送电子邮件。
通常我会在控制器创建上调用邮件程序。但是在通知控制器上我只获得索引:
def index
@activities = PublicActivity::Activity.where(recipient_id: current_user.id).includes(:owner).includes(:trackable).all.reverse
end
例如在comment.rb上我有:
include PublicActivity::Model
tracked
tracked owner: Proc.new{ |controller, model| controller.current_user }
tracked recipient: ->(controller, model) { model && model.commentable.user }
并查看comment_create
<li class="list-group-item">
<span class="glyphicon glyphicon-plus"></span>
<small class="text-muted"><%= a.created_at.strftime('%H:%M:%S %-d %B %Y') %></small><br/>
<strong><%= activity.owner ? activity.owner.name : current_user.name %></strong> commented
- <%= a.trackable.body %> - to
<%= link_to 'this item you posted.', poll_path(a.trackable.commentable_id) %>
</li>
我猜通知的创建是由gem内部完成的。
我应该在哪里打电话给邮件?
答案 0 :(得分:1)
添加新文件config/initializers/activity.rb
module PublicActivity
class Activity < inherit_orm("Activity")
after_create :send_email
def send_email
# Your code to send mail based on activity's data goes here
end
end
end
答案 1 :(得分:0)
您可以将邮件程序放在后台作业中,然后在控制器索引中调用它。