prevent double clicking of the post comment button from creating two comments

This commit is contained in:
joshua stein 2012-09-14 16:17:45 -05:00
parent 45a498ea51
commit ad1d925c61
3 changed files with 25 additions and 11 deletions

View file

@ -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 }

View file

@ -2,11 +2,11 @@ module ApplicationHelper
def errors_for(object, message=nil)
html = ""
unless object.errors.blank?
html << "<p><div class=\"flash-error\">\n"
html << "<div class=\"flash-error\">\n"
object.errors.full_messages.each do |error|
html << error << "<br>"
end
html << "</div></p>\n"
html << "</div>\n"
end
raw(html)

View file

@ -1,6 +1,10 @@
<div class="comment_form_container">
<%= 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 @@
<p></p>
<% end %>
<% if defined?(show_comment) %>
<% if show_comment.valid? %>
<ol class="comments comments1 preview">
<%= render :partial => "comments/comment",
:locals => { :comment => show_comment, :story => story } %>
</ol>
<% else %>
<%= errors_for comment %>
<% end %>
<% if defined?(show_comment) && show_comment.valid? %>
<ol class="comments comments1 preview">
<%= render :partial => "comments/comment",
:locals => { :comment => show_comment, :story => story } %>
</ol>
<% end %>
</div>