Commenting system for RoR Blogging Platform
I'm trying to design a comment system for my RoR blogging site, and I am having s开发者_Python百科ome 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!
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.
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
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
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.
精彩评论