Skip to content

Instantly share code, notes, and snippets.

@rbinsztock
Created August 8, 2012 15:45
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 rbinsztock/3296109 to your computer and use it in GitHub Desktop.
Save rbinsztock/3296109 to your computer and use it in GitHub Desktop.
<% div_for comment do %>
<h1>Commentaire ajouté par <%= comment.user.login %></h1>
<p class="comment-body"><%= comment.comment %></p>
<p>Depuis <%= time_ago_in_words(comment.created_at) %></p>
<%= link_to 'Effacer commentaire', comment, :method => :delete, :class => "delete", :remote => true %>
<% end %>
<span id="comments_count"><%= pluralize(@comment.count, "Commentaires") %></span>
<div id="comments">
<%= render :partial => @comment, :locals => { :list => true}, :object => @client %>
</div>
<hr />
<div id="comment-notice"></div>
<h2>Commentaire pour ce <%= object.class %></h2>
<% form_tag(comments_path, :remote => true) do -%>
<%= hidden_field_tag 'comment[user_id]', current_user.id %>
<%= hidden_field_tag 'comment[commentable_id]', object.id %>
<%= hidden_field_tag 'comment[commentable_type]', object.class %>
<%= text_area_tag 'comment[comment]', nil, :style => 'width:100%; height:100px;' %>
<%= submit_tag 'Ajoutez' %>
<% end %>
class CommentsController < ApplicationController
def create
@client = Client.find(params[:comment][:commentable_id])
@comment = @client.comments.new(params[:comment])
@comment.user_id = current_user.id
if @comment.save
flash[:notice] = "Votre commentaire à correctement été ajouté."
respond_to do |format|
format.js
end
else # validation fails
flash[:notice] = "Votre commentaire est vide."
redirect_to :back
end
end
def destroy
puts ">>>> Destroying a comment <<<<"
@comment = Comment.find(params[:id])
puts "found comment>>> #{@comment}"
@comment.destroy
flash[:notice] = "Votre commentaire à correctement été effacé."
respond_to do |format|
format.js
end
end
end
/* Insert a notice between the last comment and the comment form */
$("#comment-notice").html('<div class="flash notice"><%= escape_javascript(flash.delete(:notice)) %></div>');
/* Replace the count of comments */
$("#comments_count").html("<%= pluralize(Comment.count, 'Comment') %>");
/* Add the new comment to the bottom of the comments list */
$("#comments").append("<%= escape_javascript(render(@comment)) %>");
/* Highlight the new comment */
$("#comment_<%= @comment.id %>").effect("highlight", {}, 3000);
/* Reset the comment form */
$("#new_comment")[0].reset();
/* Eliminate the comment by fading it out */
$('#comment_<%= @comment.id %>').fadeOut();
/* Replace the count of comments */
$("#comments_count").html("<%= pluralize(Comment.count, 'Comentari') %>");
.............................. #show from my client controller.
<div id="tagContent6" class="tagContent">
<%= render :partial => "/comments/index", :object => @client %>
</div>
.............................
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment