如何创建一个简单的活动流?
我正在尝试实现一个简单的活动流。
Given that User abc creates a post 123
And User xyz comments on 123.
Then user abc's activity stream should look something like:
<ul>
<li>User <a href="/users/abc">abc</a> created a post <a href="/users/abc/posts/123">123</a></li>
<li>User <a href="/users/xyz">xyz</a> commented on post <a href="/users/abc/posts/123">123</a></li>
</ul>
我更喜欢填充活动模型(而不是控制器中的多个查询)。我在帖子和评论模型中添加了一个“after_create”挂钩来填充活动:
class Post < ActiveRecord
after_create do
ActivityCreate(:user_id => self.user_id, :description => "created a post")
end
end
我想存储创建路由和链接所需的特殊字段,例如 post_id 等,但我可能有更多想要的活动稍后跟踪、喜欢、不喜欢、收藏夹等,我想要一种灵活的方式来创建活动描述。
所以我的问题是,将 link_to 放入活动描述中的有效方法是什么?是否有任何 c 风格的 printf 方式来列出稍后评估的助手?
有点像这样:
description = [“User ? commented on post ?”, user_path(User.find(self.user_id)), post_path(Post.find(self.id))]
然后在模板端评估它?
I'm trying to implement a simple activity stream.
Given that User abc creates a post 123
And User xyz comments on 123.
Then user abc's activity stream should look something like:
<ul>
<li>User <a href="/users/abc">abc</a> created a post <a href="/users/abc/posts/123">123</a></li>
<li>User <a href="/users/xyz">xyz</a> commented on post <a href="/users/abc/posts/123">123</a></li>
</ul>
I prefer to populate an activity model (rather than multiple queries in a controller). I added an 'after_create' hook to both the posts and comments models to populate the activity:
class Post < ActiveRecord
after_create do
ActivityCreate(:user_id => self.user_id, :description => "created a post")
end
end
I thought of storing special fields that I would need to create routes and links, such as post_id, etc, but I may have more activities I want to track later, likes, dislikes, favorites, etc, and I want a flexible way of creating activity descriptions.
So my question is, what is an efficient way of putting the link_to into the activity description? Is there any c-style printf way of listing helpers to be evaluated later?
Sort of like this:
description = ["User ? commented on post ?", user_path(User.find(self.user_id)), post_path(Post.find(self.id))]
And then evaluate this on the template side?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是我对你的案件的第一次联想。也许它足以激励你:)
This is my first association about your case. Perhaps it inspires you enough :)