Skip to content

Instantly share code, notes, and snippets.

@mooreniemi
Last active December 17, 2015 18:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mooreniemi/5651232 to your computer and use it in GitHub Desktop.
Save mooreniemi/5651232 to your computer and use it in GitHub Desktop.
# comments.js.coffee
jQuery ->
# Create a comment
$(".comment-form")
.on "ajax:beforeSend", (evt, xhr, settings) ->
$(this).find('textarea')
.addClass('uneditable-input')
.attr('disabled', 'disabled');
.on "ajax:success", (evt, data, status, xhr) ->
$(this).find('textarea')
.removeClass('uneditable-input')
.removeAttr('disabled', 'disabled')
.val('');
$(xhr.responseText).hide().insertAfter($(this)).show('slow')
# Delete a comment
$(document)
.on "ajax:beforeSend", ".comment", ->
$(this).fadeTo('fast', 0.5)
.on "ajax:success", ".comment", ->
$(this).hide('fast')
.on "ajax:error", ".comment", ->
$(this).fadeTo('fast', 1)
#_comment.html.erb
<div class="comment" id=<%=comment.id %>>
<div class="well"><%= comment.body %><br>
<%= comment.user.username %>
<small><%= comment.updated_at.strftime('%b %d, %Y at %I:%M %p') %></small>
<%= link_to "×", comment_path(comment), :method => :delete, :remote => true, :confirm => "Are you sure you want to remove this comment?", :disable_with => "×", :class => 'close' %></div>
</div>
#_form.html.erb
<h3>Add Comment</h3>
<div class="comment-form">
<%= simple_form_for @new_comment, :remote => true do |f| %>
<%= f.error_notification %>
<%= f.input :body, :input_html => {:rows =>"4"}, :label => false %>
<%= f.hidden_field :commentable_id, :value => @new_comment.commentable_id %>
<%= f.hidden_field :commentable_type, :value => @new_comment.commentable_type %>
<%= f.submit "Submit", class: "btn btn-primary", :disable_with => "Submitting..." %>
<% end %>
</div>
<br clear="both">
#show.html.erb
<%= render :partial => 'comments/form', :locals => { :comment => @new_comment } %>
<%= render :partial => 'comments/comment', :collection => @comments, :as => :comment %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment