开发者

anonymous and registered user implementation with acts_as_commentable?

I am using acts_as_commentable and am curious if anyone has any good ideas on how to allow for anonymous and registered users to post comments? Meaning, if a registered user is authentica开发者_StackOverflow中文版ted, I want the comment to be marked with their name, etc. But I also want an anonymous user to be able to comment and have a name and email address recorded. I am using Devise for authentication.

I have an idea on how to make this work but it feels a little hacky to me. Wondering if anyone has any thoughts.


I don't know your plugin, but if you use this one (https://github.com/jackdempsey/acts_as_commentable), it seems very basic...

The Comment model has a relation to a user which is not mandatory. So in your new comment form, I would just add two text_field_tags if the user is not logged (text_field_tag :first_name, text_field_tag :last_name).

And I'd just write the create action for comments like this :

def create
  @comment = Comment.new(:commentable => @your_object, :user => current_user, :first_name => params[:first_name], :last_name => params[:last_name])
  ...
end

if the user is not logged, current_user will be nil and that won't cause any problem.

You can write an helper method to display the name for a comment depending it has a user or not like this...

# Displays the user's login if any or else the first name and last name
def displayed_name(comment)
  comment.user ? comment.user.login : "#{comment.first_name} #{comment.last_name}"
end
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜