Question: Create comment from controller
Opened this issue · 2 comments
mohamed-benali commented
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
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
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