From ad1d925c618ee29b64203c8b4aae8ea6c504bf0e Mon Sep 17 00:00:00 2001 From: joshua stein Date: Fri, 14 Sep 2012 16:17:45 -0500 Subject: [PATCH] prevent double clicking of the post comment button from creating two comments --- app/controllers/comments_controller.rb | 14 ++++++++++++++ app/helpers/application_helper.rb | 4 ++-- app/views/comments/_commentbox.html.erb | 18 +++++++++--------- 3 files changed, 25 insertions(+), 11 deletions(-) diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index 983881c..9b96659 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -26,6 +26,20 @@ class CommentsController < ApplicationController comment.thread_id = Keystore.incremented_value_for("thread_id") end + # prevent double-clicks of the post button + if !params[:preview].present? && + (pc = Comment.find_by_story_id_and_user_id_and_parent_comment_id(story.id, + @user.id, comment.parent_comment_id)) + if (Time.now - pc.created_at) < 5.minutes + comment.errors.add(:comment, "^You have already posted a comment " << + "here recently.") + + return render :partial => "commentbox", :layout => false, + :content_type => "text/html", :locals => { :story => story, + :comment => comment } + end + end + if comment.valid? && !params[:preview].present? && comment.save comment.current_vote = { :vote => 1 } diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index e90277c..7600b91 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -2,11 +2,11 @@ module ApplicationHelper def errors_for(object, message=nil) html = "" unless object.errors.blank? - html << "

\n" + html << "
\n" object.errors.full_messages.each do |error| html << error << "
" end - html << "

\n" + html << "
\n" end raw(html) diff --git a/app/views/comments/_commentbox.html.erb b/app/views/comments/_commentbox.html.erb index af3095c..3f727dd 100644 --- a/app/views/comments/_commentbox.html.erb +++ b/app/views/comments/_commentbox.html.erb @@ -1,6 +1,10 @@
<%= form_tag((comment.new_record? ? "/comments/post_to/#{story.short_id}" : "/comments/#{comment.short_id}/update"), { :id => "comment_form" }) do |f| %> + <% if comment && comment.errors.any? %> + <%= errors_for comment %> + <% end %> + <% if comment.parent_comment_short_id.present? %> <%= hidden_field_tag "parent_comment_short_id", comment.parent_comment_short_id %> @@ -31,14 +35,10 @@

<% end %> -<% if defined?(show_comment) %> - <% if show_comment.valid? %> -
    - <%= render :partial => "comments/comment", - :locals => { :comment => show_comment, :story => story } %> -
- <% else %> - <%= errors_for comment %> - <% end %> +<% if defined?(show_comment) && show_comment.valid? %> +
    + <%= render :partial => "comments/comment", + :locals => { :comment => show_comment, :story => story } %> +
<% end %>