prevent double clicking of the post comment button from creating two comments
This commit is contained in:
parent
45a498ea51
commit
ad1d925c61
|
@ -26,6 +26,20 @@ class CommentsController < ApplicationController
|
||||||
comment.thread_id = Keystore.incremented_value_for("thread_id")
|
comment.thread_id = Keystore.incremented_value_for("thread_id")
|
||||||
end
|
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
|
if comment.valid? && !params[:preview].present? && comment.save
|
||||||
comment.current_vote = { :vote => 1 }
|
comment.current_vote = { :vote => 1 }
|
||||||
|
|
||||||
|
|
|
@ -2,11 +2,11 @@ module ApplicationHelper
|
||||||
def errors_for(object, message=nil)
|
def errors_for(object, message=nil)
|
||||||
html = ""
|
html = ""
|
||||||
unless object.errors.blank?
|
unless object.errors.blank?
|
||||||
html << "<p><div class=\"flash-error\">\n"
|
html << "<div class=\"flash-error\">\n"
|
||||||
object.errors.full_messages.each do |error|
|
object.errors.full_messages.each do |error|
|
||||||
html << error << "<br>"
|
html << error << "<br>"
|
||||||
end
|
end
|
||||||
html << "</div></p>\n"
|
html << "</div>\n"
|
||||||
end
|
end
|
||||||
|
|
||||||
raw(html)
|
raw(html)
|
||||||
|
|
|
@ -1,6 +1,10 @@
|
||||||
<div class="comment_form_container">
|
<div class="comment_form_container">
|
||||||
<%= form_tag((comment.new_record? ? "/comments/post_to/#{story.short_id}" :
|
<%= form_tag((comment.new_record? ? "/comments/post_to/#{story.short_id}" :
|
||||||
"/comments/#{comment.short_id}/update"), { :id => "comment_form" }) do |f| %>
|
"/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? %>
|
<% if comment.parent_comment_short_id.present? %>
|
||||||
<%= hidden_field_tag "parent_comment_short_id",
|
<%= hidden_field_tag "parent_comment_short_id",
|
||||||
comment.parent_comment_short_id %>
|
comment.parent_comment_short_id %>
|
||||||
|
@ -31,14 +35,10 @@
|
||||||
|
|
||||||
<p></p>
|
<p></p>
|
||||||
<% end %>
|
<% end %>
|
||||||
<% if defined?(show_comment) %>
|
<% if defined?(show_comment) && show_comment.valid? %>
|
||||||
<% if show_comment.valid? %>
|
<ol class="comments comments1 preview">
|
||||||
<ol class="comments comments1 preview">
|
<%= render :partial => "comments/comment",
|
||||||
<%= render :partial => "comments/comment",
|
:locals => { :comment => show_comment, :story => story } %>
|
||||||
:locals => { :comment => show_comment, :story => story } %>
|
</ol>
|
||||||
</ol>
|
|
||||||
<% else %>
|
|
||||||
<%= errors_for comment %>
|
|
||||||
<% end %>
|
|
||||||
<% end %>
|
<% end %>
|
||||||
</div>
|
</div>
|
||||||
|
|
Loading…
Reference in a new issue