【发布时间】:2011-09-19 01:32:48
【问题描述】:
我有一个看起来像这样的观察者:
class CommentObserver < ActiveRecord::Observer
include ActionView::Helpers::UrlHelper
def after_create(comment)
message = "#{link_to comment.user.full_name, user_path(comment.user)} commented on #{link_to 'your photo',photo_path(comment.photo)} of #{comment.photo.location(:min)}"
Notification.create(:user=>comment.photo.user,:message=>message)
end
end
基本上,我使用它所做的只是在有人对他们的一张照片发表评论时为某个用户创建一条简单的通知消息。
此操作失败并显示错误消息:
NoMethodError (undefined method `link_to' for #<CommentObserver:0x00000102fe9810>):
我原以为包含ActionView::Helpers::UrlHelper 会解决这个问题,但它似乎没有任何效果。
那么,我怎样才能在我的观察者中包含 URL 帮助程序,或者以其他方式呈现呢?我很乐意将“消息视图”移动到部分或其他内容中,但是观察者没有关联的视图可以将其移动到...
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-3 observer-pattern link-to url-helper