lml / commontator

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

Double_posted comment error #76

Closed JBenzie closed 8 years ago

JBenzie commented 9 years ago

After implementing the Commontator gem as described in the documentation, I try to add a comment to a user's post and I get the following error:

This comment could not be posted because: Comment translation missing: en.commentator.comment.errors.double_posted"

The comment does get added to the post, but it seems as if it is trying to submit it twice or something. From there, you click cancel or refresh and everything is fine. However, I think this could cause confusion amongst users when they're constantly seeing this error when making a comment.

Any ideas? Thanks!

Dantemss commented 9 years ago

Hello. To fix the translation issue for now you can manually copy commontator's locales to your app using rake commontator:copy:locales.

Does the double posting error happen every single time you post some comment in your app? Or only in some cases?

JBenzie commented 9 years ago

It happens every single time

JBenzie commented 9 years ago

Your suggestion did not fix the issue. I still get the duplicate comment error.

Dantemss commented 9 years ago

Sorry, but I can't help you without knowing more about your app. Commontator doesn't have much magic in it.

Posting a comment is a simple create action: https://github.com/lml/commontator/blob/master/app/controllers/commontator/comments_controller.rb#L23

The comment validates that its body is unique on creation/update: https://github.com/lml/commontator/blob/master/app/models/commontator/comment.rb#L12

If you want to debug this, you could try copying the comment object into your app, removing the validation and seeing if you get 2 comments posted in a row. If that's the case, it could be an issue of including jquery_ujs twice or something odd like that.

JBenzie commented 9 years ago

I removed the comment uniqueness validation and the error disappeared. However, clicking the "Post Comment" button a single time creates anywhere from 2 to 10 identical comments (which is obviously why the duplicate comment error was being displayed). Below is what showed up in the console most recently:

Started POST "/commontator/threads/5/comments" for 127.0.0.1 at 2015-09-08 11:59:05 -0400
Processing by Commontator::CommentsController#create as JS
  Parameters: {"utf8"=>"✓", "per_page"=>"", "comment"=>{"body"=>"test"}, "commit"=>"Post Comment", "
thread_id"=>"5"}
  User Load (0.0ms)  SELECT  "users".* FROM "users"  WHERE "users"."id" = 2  ORDER BY "users"."id" A
SC LIMIT 1
  Commontator::Thread Load (0.0ms)  SELECT  "commontator_threads".* FROM "commontator_threads"  WHER
E "commontator_threads"."id" = ? LIMIT 1  [["id", 5]]
  Post Load (1.0ms)  SELECT  "posts".* FROM "posts"  WHERE "posts"."id" = ? LIMIT 1  [["id", 5]]
   (0.0ms)  begin transaction
  SQL (1.0ms)  INSERT INTO "commontator_comments" ("body", "created_at", "creator_id", "creator_type
", "thread_id", "updated_at") VALUES (?, ?, ?, ?, ?, ?)  [["body", "test"], ["created_at", "2015-09-
08 15:59:05.627452"], ["creator_id", 2], ["creator_type", "User"], ["thread_id", 5], ["updated_at",
"2015-09-08 15:59:05.627452"]]
   (6.0ms)  commit transaction
  Commontator::Subscription Load (0.0ms)  SELECT "commontator_subscriptions".* FROM "commontator_sub
scriptions"  WHERE "commontator_subscriptions"."thread_id" = ?  [["thread_id", 5]]
  Commontator::Comment Load (0.0ms)  SELECT "commontator_comments".* FROM "commontator_comments"  WH
ERE "commontator_comments"."thread_id" = ?  ORDER BY created_at ASC  [["thread_id", 5]]
  User Load (0.0ms)  SELECT  "users".* FROM "users"  WHERE "users"."id" = ? LIMIT 1  [["id", 2]]
  Commontator::Thread Load (0.0ms)  SELECT  "commontator_threads".* FROM "commontator_threads"  WHER
E "commontator_threads"."id" = ? LIMIT 1  [["id", 5]]
  Post Load (1.0ms)  SELECT  "posts".* FROM "posts"  WHERE "posts"."id" = ? LIMIT 1  [["id", 5]]
  Commontator::Comment Load (0.0ms)  SELECT  "commontator_comments".* FROM "commontator_comments"  W
HERE "commontator_comments"."thread_id" = ?  ORDER BY "commontator_comments"."id" DESC LIMIT 1  [["t
hread_id", 5]]
  CACHE (0.0ms)  SELECT  "commontator_comments".* FROM "commontator_comments"  WHERE "commontator_co
mments"."thread_id" = ?  ORDER BY "commontator_comments"."id" DESC LIMIT 1  [["thread_id", 5]]
  Rendered C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/commontator-4.10.5/app/views/commont
ator/comments/_actions.html.erb (5.0ms)
  Rendered C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/commontator-4.10.5/app/views/commont
ator/comments/_votes.html.erb (0.0ms)
  Rendered C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/commontator-4.10.5/app/views/commont
ator/comments/_body.html.erb (0.0ms)
  Rendered C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/commontator-4.10.5/app/views/commont
ator/comments/_show.html.erb (25.0ms)
  CACHE (0.0ms)  SELECT  "users".* FROM "users"  WHERE "users"."id" = ? LIMIT 1  [["id", 2]]
  CACHE (0.0ms)  SELECT  "commontator_threads".* FROM "commontator_threads"  WHERE "commontator_thre
ads"."id" = ? LIMIT 1  [["id", 5]]
  CACHE (0.0ms)  SELECT  "posts".* FROM "posts"  WHERE "posts"."id" = ? LIMIT 1  [["id", 5]]
  CACHE (0.0ms)  SELECT  "commontator_comments".* FROM "commontator_comments"  WHERE "commontator_co
mments"."thread_id" = ?  ORDER BY "commontator_comments"."id" DESC LIMIT 1  [["thread_id", 5]]
  CACHE (0.0ms)  SELECT  "commontator_comments".* FROM "commontator_comments"  WHERE "commontator_co
mments"."thread_id" = ?  ORDER BY "commontator_comments"."id" DESC LIMIT 1  [["thread_id", 5]]
  Rendered C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/commontator-4.10.5/app/views/commont
ator/comments/_actions.html.erb (7.0ms)
  Rendered C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/commontator-4.10.5/app/views/commont
ator/comments/_votes.html.erb (0.0ms)
  Rendered C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/commontator-4.10.5/app/views/commont
ator/comments/_body.html.erb (0.0ms)
  Rendered C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/commontator-4.10.5/app/views/commont
ator/comments/_show.html.erb (23.0ms)
  CACHE (0.0ms)  SELECT  "users".* FROM "users"  WHERE "users"."id" = ? LIMIT 1  [["id", 2]]
  CACHE (0.0ms)  SELECT  "commontator_threads".* FROM "commontator_threads"  WHERE "commontator_thre
ads"."id" = ? LIMIT 1  [["id", 5]]
  CACHE (0.0ms)  SELECT  "posts".* FROM "posts"  WHERE "posts"."id" = ? LIMIT 1  [["id", 5]]
  CACHE (0.0ms)  SELECT  "commontator_comments".* FROM "commontator_comments"  WHERE "commontator_co
mments"."thread_id" = ?  ORDER BY "commontator_comments"."id" DESC LIMIT 1  [["thread_id", 5]]
  CACHE (0.0ms)  SELECT  "commontator_comments".* FROM "commontator_comments"  WHERE "commontator_co
mments"."thread_id" = ?  ORDER BY "commontator_comments"."id" DESC LIMIT 1  [["thread_id", 5]]
  Rendered C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/commontator-4.10.5/app/views/commont
ator/comments/_actions.html.erb (5.0ms)
  Rendered C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/commontator-4.10.5/app/views/commont
ator/comments/_votes.html.erb (0.0ms)
  Rendered C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/commontator-4.10.5/app/views/commont
ator/comments/_body.html.erb (0.0ms)
  Rendered C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/commontator-4.10.5/app/views/commont
ator/comments/_show.html.erb (23.0ms)
  CACHE (0.0ms)  SELECT  "users".* FROM "users"  WHERE "users"."id" = ? LIMIT 1  [["id", 2]]
  CACHE (0.0ms)  SELECT  "commontator_threads".* FROM "commontator_threads"  WHERE "commontator_thre
ads"."id" = ? LIMIT 1  [["id", 5]]
  CACHE (0.0ms)  SELECT  "posts".* FROM "posts"  WHERE "posts"."id" = ? LIMIT 1  [["id", 5]]
  CACHE (0.0ms)  SELECT  "commontator_comments".* FROM "commontator_comments"  WHERE "commontator_co
mments"."thread_id" = ?  ORDER BY "commontator_comments"."id" DESC LIMIT 1  [["thread_id", 5]]
  CACHE (0.0ms)  SELECT  "commontator_comments".* FROM "commontator_comments"  WHERE "commontator_co
mments"."thread_id" = ?  ORDER BY "commontator_comments"."id" DESC LIMIT 1  [["thread_id", 5]]
  Rendered C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/commontator-4.10.5/app/views/commont
ator/comments/_actions.html.erb (7.0ms)
  Rendered C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/commontator-4.10.5/app/views/commont
ator/comments/_votes.html.erb (0.0ms)
  Rendered C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/commontator-4.10.5/app/views/commont
ator/comments/_body.html.erb (0.0ms)
  Rendered C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/commontator-4.10.5/app/views/commont
ator/comments/_show.html.erb (22.0ms)
  Rendered C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/commontator-4.10.5/app/views/commont
ator/comments/_list.html.erb (113.0ms)
  Rendered C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/commontator-4.10.5/app/views/commont
ator/threads/_reply.html.erb (1.0ms)
  Rendered C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/commontator-4.10.5/app/views/commont
ator/threads/_show.html.erb (125.0ms)
  Rendered C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/commontator-4.10.5/app/views/commont
ator/threads/_show.js.erb (129.0ms)
  Rendered C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/commontator-4.10.5/app/views/commont
ator/comments/create.js.erb (133.0ms)
Completed 200 OK in 168ms (Views: 146.0ms | ActiveRecord: 9.0ms)

Note: this was after I typed a single comment, "test", and clicked the "Post Comment" button once. I've also tried re-installing the gem.

Dantemss commented 9 years ago

Hello, is the same situation happening to other (unrelated) requests in the app (multiple renders for a single request?)

JBenzie commented 9 years ago

No, this only occurs when adding new comments. In my searching, I've found an old issue (Issue #23) that seems to be the same as what I'm experiencing. I don't know if that helps you at all, but thought I'd mention it.

Dantemss commented 9 years ago

It seems as if the controller is trying to respond with both the html and the JS for some reason. If you temporarily disable JS on your browser, does the issue fix itself?

JBenzie commented 9 years ago

Yes, that worked. After disabling JS on the browser, only 1 comment was posted.

Dantemss commented 9 years ago

Hm could be some bug with respond_to blocks with Rails running on windows, or maybe jquery_ujs being included in the application more than once... For now you should be able to get around this by adding no_remote: true to this hash: https://github.com/lml/commontator/blob/master/app/views/commontator/comments/new.js.erb#L3

That should make the comment posting process as HTML.

Dantemss commented 9 years ago

no_remote: true needs to go inside the :locals hash (and you can copy that file into your app as app/views/commontator/comments/new.js.erb)

JBenzie commented 9 years ago

Do I add it like so?

 render :partial => 'form',
         :locals => { :comment => @comment,
                      :thread => @thread,
                      :per_page => @per_page
                      no_remote: true }) %>");

As this is what I've done but now I cannot add new comments. The new comment form does not appear when I click the link. Forgive me, I'm new to Ruby on Rails, so I may be making noob mistakes during this troubleshooting.

Dantemss commented 9 years ago

Just missing a comma on the line above... it might also be better not to mix the old and new hash syntax I suppose, so:

 render :partial => 'form',
         :locals => { :comment => @comment,
                      :thread => @thread,
                      :per_page => @per_page,
                      :no_remote => true }) %>");
JBenzie commented 9 years ago

Seems to be working now. Thanks for the help!

Feel free to close the issue if/when you see fit. Not sure if you were going to continue working on this or if the fix above was the final solution. In any case, I'll leave it open for you to close when necessary.