Skip to content

Instantly share code, notes, and snippets.

@mooreniemi
Last active December 17, 2015 18:29
Show Gist options
  • Save mooreniemi/5653616 to your computer and use it in GitHub Desktop.
Save mooreniemi/5653616 to your computer and use it in GitHub Desktop.
<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>
<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">
(function() {
jQuery(function() {
$(".comment-form").on("ajax:beforeSend", function(evt, xhr, settings) {
return $(this).find('textarea').addClass('uneditable-input').attr('disabled', 'disabled');
}).on("ajax:success", function(evt, data, status, xhr) {
$(this).find('textarea').removeClass('uneditable-input').removeAttr('disabled', 'disabled').val('');
return $(xhr.responseText).hide().insertAfter($(this)).show('slow');
});
return $(document).on("ajax:beforeSend", ".comment", function() {
return $(this).fadeTo('fast', 0.5);
}).on("ajax:success", ".comment", function() {
return $(this).hide('fast');
}).on("ajax:error", ".comment", function() {
return $(this).fadeTo('fast', 1);
});
});
}).call(this);
# 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)
# comments_controller.rb
class CommentsController < ApplicationController
def create
@comment_hash = params[:comment]
@obj = @comment_hash[:commentable_type].constantize.find(@comment_hash[:commentable_id])
# Not implemented: check to see whether the user has permission to create a comment on this object
@comment = Comment.build_from(@obj, current_user, @comment_hash[:body])
if @comment.save
render :partial => "comments/comment", :locals => { :comment => @comment }, :layout => false, :status => :created
else
render :js => "alert('error saving comment');"
end
end
def destroy
@comment = Comment.find(params[:id])
if @comment.destroy
render :json => @comment, :status => :ok
else
render :js => "alert('error deleting comment');"
end
end
end
<div class="row-fluid">
<div class="span8">
<div class="well">
<% for pinimage in @pin.pin_images do %>
<%= image_tag pinimage.photo unless pinimage.photo_file_size.nil? %>
<%= pinimage.caption.to_s %>
<% end %>
<br><% if current_user == @pin.user || current_user.admin == true %> <%= link_to 'Edit', edit_pin_path(@pin) %> | <% end %>
<%= link_to 'Back', pins_path %>
</div>
</div>
<div class="span3">
<ul class="unstyled">
<li><b>Surgeon:</b>
Dr. <%= @pin.surgeon %></li>
<li><b>Procedure:</b>
<%= @pin.procedure %></li>
<li><b>Cost:</b>
<%= @pin.cost %></li>
<li><b>Want revision?:</b>
<li><%= @pin.revision %></li>
<li><b>Description:</b>
<%= @pin.details %></li>
</ul>
<small><b>Submitter:</b> <%= @pin.username %></small>
</div>
<br clear="both" />
<%= 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