lml / commontator

A Rails engine for comments
MIT License
354 stars 99 forks source link

Question: Create comment from controller #154

Open mohamed-benali opened 4 years ago

mohamed-benali commented 4 years ago

How can i create a comment from a controller. I want to create a comment each time i edit a post(the comment needs to be attached to the thread of the post i edited). The documentation don't say anything abot creating a comment from the code. How should i do it?

Dantemss commented 4 years ago

You can do something like https://github.com/lml/commontator/blob/master/app/controllers/commontator/comments_controller.rb#L37-L39 You can get the thread with object.commontator_thread

Other lines in this method are access control (which you probably already handle yourself), subscriptions and comment nesting, which you may not need.

mohamed-benali commented 4 years ago

Thank you

Just for completness im gonna post how its done.

I putted this code on my post controller

# Code
@commontator_thread = @post.commontator_thread
@commontator_user = User.find(session["logged_user"]) 
@comment = Commontator::Comment.new(
    thread: @commontator_thread, creator: @commontator_user, body: "message text"
)
if @comment.save
    # Code
end