And*_*rew 5 ruby-on-rails link-to url-helper observer-pattern ruby-on-rails-3
我有一个看起来像这样的观察者:
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
Run Code Online (Sandbox Code Playgroud)
基本上我正在使用它做的是当有人在他们的一张照片上发表评论时为某个用户创建一个简单的通知消息.
这失败并显示错误消息:
NoMethodError (undefined method `link_to' for #<CommentObserver:0x00000102fe9810>):
Run Code Online (Sandbox Code Playgroud)
我原ActionView::Helpers::UrlHelper以为包括会解决这个问题,但似乎没有效果.
那么,我如何在我的观察者中包含URL助手,或者以其他方式呈现这个?我很乐意将"消息视图"移动到局部或类似的东西中,但是观察者没有相关的视图来将其移动到......