RoR博客平台评论系统

发布于 2024-11-06 07:59:31 字数 663 浏览 4 评论 0 原文

我正在尝试为我的 RoR 博客网站设计一个评论系统,但我在架构方面遇到了一些概念问题。就模型而言,我有博客文章、用户和评论。

  • 一个用户有_许多博客文章
  • 一个博客文章属于一个用户
  • 一个博客文章有_许多评论
  • 评论可能属于也可能不属于注册的用户(我希望未在该网站注册的人能够也评论一下)。

我的问题是这样的:为了强制评论和博客文章之间的链接,我通过博客帖子关联(@blogpost.comments.build)创建每个新评论(@comment)强>(:args))。但是,我不知道如何将特定的注册用户与他/她的评论相关联。我为 Comment 模型保留了 attr_accessibleuser_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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

谜泪 2024-11-13 07:59:31

假设:

User has_many comments
Comment belongs_to user

在您的控制器中保存评论时,您可以简单地执行以下操作:

@comment.user = current_user if current_user
@comment.save

如果评论是由未注册用户完成的,则 @comment.user 保持为空。

Assuming:

User has_many comments
Comment belongs_to user

In your controller when saving the comment, you can simply do:

@comment.user = current_user if current_user
@comment.save

If the comment is done by an unregistered user @comment.user just stays empty.

酒儿 2024-11-13 07:59:31

你可以建立一个关联:

User has_many comments through blog_posts

所以,现在你可以这样做:

current_user.comments

另一种方法是通过 blog_post:

current_user.blog_post.comments

此外,你可以使用很好的 act_as_commentable 插件:)

https://github.com/jackdempsey/acts_as_commentable

You can just have an association :

User has_many comments through blog_posts

So, now you can do :

current_user.comments

Another way to do it is via blog_post:

current_user.blog_post.comments

Moreover, you can use the nice act_as_commentable plugin :)

https://github.com/jackdempsey/acts_as_commentable

哆啦不做梦 2024-11-13 07:59:31

如果您可以在保存或发布新评论方法中访问当前登录的用户,则无需将 user_id 设置为 attr_accessible

如果他们没有登录,那么您预计当前用户为空/错误。

如果您使用任何身份验证插件(例如 authlogic 或 devise),这应该可用。根据我使用 authlogic 的经验,您的 ApplicationController 中通常有一个 current_user 方法。

class ApplicationController
helper_method :current_user_session, :current_user

private
  def current_user_session
    return @current_user_session if defined?(@current_user_session)
    @current_user_session = UserSession.find
  end

  def current_user
    return @current_user if defined?(@current_user)
    @current_user = current_user_session && current_user_session.user
  end
end

以上代码来自 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.

class ApplicationController
helper_method :current_user_session, :current_user

private
  def current_user_session
    return @current_user_session if defined?(@current_user_session)
    @current_user_session = UserSession.find
  end

  def current_user
    return @current_user if defined?(@current_user)
    @current_user = current_user_session && current_user_session.user
  end
end

Above code from the Authlogic quick example

病毒体 2024-11-13 07:59:31

您可以在 Comment 和 User 之间添加关联,然后使用 current_user 创建评论:

# User.rb
has_many :comments

# Comment
belongs_to :user

设置关联仅真正添加关联方法,因此在没有登录用户的情况下创建 Comment 没有问题。您不想将 current_user 的注释构建为 current_user.comments.create(...),因为如果没有人登录,这会引发 NilClass 错误

@user = current_user # @user should be nil if commenter is not logged in
# be fancy and use a block
@blogpost.comments.create(params[:comment]) do |c|
  c.user = @user
end

。对 Comment 中的 User 进行验证,nil 用户应该可以顺利通过。

You can add an association between Comment and User, then create the comment with current_user:

# User.rb
has_many :comments

# Comment
belongs_to :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.

@user = current_user # @user should be nil if commenter is not logged in
# be fancy and use a block
@blogpost.comments.create(params[:comment]) do |c|
  c.user = @user
end

As long as there is no validation for User in Comment, the nil user should just pass through without trouble.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文