ruby-on-rails – Rails:在Observer中使用URL Helper
发布时间:2020-12-17 03:27:41 所属栏目:百科 来源:网络整理
导读:我有一个看起来像这样的观察者: 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
我有一个看起来像这样的观察者:
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助手,或者以其他方式呈现这个?我很乐意将“消息视图”移动到局部或类似的东西中,但是观察者没有相关的视图来将其移动到…… 解决方法
为什么不在将消息呈现到页面然后使用类似的东西进行缓存时构建消息?
<% cache do %> <%= render user.notifications %> <% end %> 这样可以省去你在观察者中进行黑客操作,并且在Rails中更符合“标准”. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |