我正在尝试为我的 RoR 博客网站设计一个评论系统,但我在架构方面遇到了一些概念问题。就模型而言,我有博客文章、用户和评论。
- 一个用户有_许多博客文章
- 一个博客文章属于一个用户
- 一个博客文章有_许多评论
- 评论可能属于也可能不属于注册的用户(我希望未在该网站注册的人能够也评论一下)。
我的问题是这样的:为了强制评论和博客文章之间的链接,我通过博客帖子关联(@blogpost.comments.build)创建每个新评论(@comment)强>(:args))。但是,我不知道如何将特定的注册用户与他/她的评论相关联。我为 Comment 模型保留了 attr_accessible 的 user_id 属性,因为我想防止人们将评论归因于错误的用户。
关于如何最好地实现具有这种关系的评论系统有什么想法吗?非常感谢!
I'm trying to design a comment system for my RoR blogging site, and I am having some conceptual problems with the architecture. As far as models are concerned, I have Blogposts, Users, and Comments.
- A User has_many Blogposts
- A Blogpost belongs_to one User
- A Blogpost has_many Comments
- A Comment may or may not belong to a registered User (I want people not registered with the site to be able to comment as well).
My question is this: in order to enforce the link between a comment and a blogpost, I create each new comment (@comment) through the blogpost association (@blogpost.comments.build(:args)). However, I do not know how to associate a particular registered User with his/her comment. I left the user_id attribute OUT of the attr_accessible for the Comment model because I wanted to prevent the possibility of people attributing comments to the wrong users.
Any ideas on how best to implement a commenting system with such a relation? Thanks so much in advance!
发布评论
评论(4)
假设:
在您的控制器中保存评论时,您可以简单地执行以下操作:
如果评论是由未注册用户完成的,则
@comment.user
保持为空。Assuming:
In your controller when saving the comment, you can simply do:
If the comment is done by an unregistered user
@comment.user
just stays empty.你可以建立一个关联:
所以,现在你可以这样做:
另一种方法是通过 blog_post:
此外,你可以使用很好的 act_as_commentable 插件:)
https://github.com/jackdempsey/acts_as_commentable
You can just have an association :
So, now you can do :
Another way to do it is via blog_post:
Moreover, you can use the nice act_as_commentable plugin :)
https://github.com/jackdempsey/acts_as_commentable
如果您可以在保存或发布新评论方法中访问当前登录的用户,则无需将 user_id 设置为 attr_accessible。
如果他们没有登录,那么您预计当前用户为空/错误。
如果您使用任何身份验证插件(例如 authlogic 或 devise),这应该可用。根据我使用 authlogic 的经验,您的 ApplicationController 中通常有一个 current_user 方法。
以上代码来自 Authlogic 快速示例
There's no need to have user_id as attr_accessible if you have access to the currently logged in user in your save or post new comment methods.
If they aren't logged in then you expect current user to be empty / false.
This should be available if you're using any of the authentication plugins such as authlogic or devise. In my experience with authlogic you typically have a current_user method in your ApplicationController.
Above code from the Authlogic quick example
您可以在 Comment 和 User 之间添加关联,然后使用 current_user 创建评论:
设置关联仅真正添加关联方法,因此在没有登录用户的情况下创建 Comment 没有问题。您不想将 current_user 的注释构建为
current_user.comments.create(...)
,因为如果没有人登录,这会引发 NilClass 错误。对 Comment 中的 User 进行验证,nil 用户应该可以顺利通过。
You can add an association between Comment and User, then create the comment with current_user:
Setting up the associations only really adds the association methods, so there's no problem with creating Comment without a logged in user. You don't want to build the comment off of current_user as
current_user.comments.create(...)
, because that will throw a NilClass error if nobody is logged in.As long as there is no validation for User in Comment, the nil user should just pass through without trouble.