Skip to content

Instantly share code, notes, and snippets.

@spacerobotTR
Created May 5, 2018 12:22
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 spacerobotTR/d77d38ced49224ae11d80ed09aa4ed8b to your computer and use it in GitHub Desktop.
Save spacerobotTR/d77d38ced49224ae11d80ed09aa4ed8b to your computer and use it in GitHub Desktop.
Rails Ajax Comments
<% @comments.each do |comment| %>
<div class="feed-element">
<div class="media-body" style="font-size:14px;">
<span class="pull-right text-navy"><%= time_ago_in_words(comment.updated_at) %> ago</span>
<strong><%= comment.user.username %></strong> updated this service request.
<br>
<small class="text-muted"><%= @servicerequest.created_at.to_time.strftime('%B %e at %l:%M %p') %></small>
<div class="well" style="font-size:14px;">
<%= comment.content.html_safe %>
<% if current_user.username == comment.user.username %>
<div class="actions">
<%= link_to "Edit", edit_servicerequest_comment_path(comment.servicerequest, comment), :remote => true, class: 'btn btn-xs btn-primary' %>
<!-- <%= link_to "Edit", [@servicerequest, comment] , method: :patch, :remote => true, class: 'btn btn-xs btn-primary' %> -->
<%= link_to "Delete", [@servicerequest, comment] , method: :delete, :remote => true, data: { confirm: 'Are you sure?' }, class: 'btn btn-xs btn-outline btn-inverse' %>
</div>
<% end %>
</div>
</div>
</div>
<% end %>
<%= form_for([@servicerequest, @comment || @servicerequest.comments.build], remote: true) do |f| %>
<div>
<%= error_messages_for(f.object) %>
</div>
<div>
<%= f.text_field :content, class: 'form-control commentbox', autofocus: true, :id => 'commentbox' %>
</div>
<div style="padding-top:20px;">
<%= button_to ( f.object.new_record? ? "Create Comment" : "Update Comment"), servicerequest_comments_path(servicerequest_id: @servicerequest), remote: true,
class: 'btn btn-primary' %>
</div>
<% end %>
class CommentsController < ApplicationController
before_action :find_servicerequest
before_action :find_comment, only: [:destroy, :edit, :update, :comment_owner]
before_action :comment_owner, only: [:destroy, :edit, :update]
def new
@comment = @servicerequest.comments.build
end
def create
@comments = Comment.where(servicerequest_id: @servicerequest).order("created_at DESC")
@comment = @servicerequest.comments.new(params[:comment].permit(:content))
@comment.user_id = current_user.id
respond_to do |format|
if @comment.save
format.js
format.html { redirect_to servicerequest_path(@servicerequest), notice: 'Comment was successfully created.' }
format.json { render :show, status: :created, location: [@servicerequest, @upload] }
else
format.html { render "servicerequests/show" }
format.json { render json: @comment.errors, status: :unprocessable_entity }
end
end
end
def edit
respond_to do |format|
format.js
end
end
def update
@comments = Comment.where(servicerequest_id: @servicerequest).order("created_at DESC")
respond_to do |format|
if @comment.update(params[:comment].permit(:content))
format.js
else
format.js
end
end
end
def destroy
@comments = Comment.where(servicerequest_id: @servicerequest).order("created_at DESC")
@comment.destroy
respond_to do |format|
format.html { redirect_to servicerequest_path(@servicerequest), notice: 'Servicerequest was successfully destroyed.' }
format.js
end
end
private
def find_servicerequest
@servicerequest = Servicerequest.find(params[:servicerequest_id])
end
def find_comment
@comment = @servicerequest.comments.find(params[:id])
end
def comment_owner
unless current_user.id == @comment.user_id
flash[:notice] = "You cannot modify a different users udpates."
redirect_to @servicerequest
end
end
end
$('#commentform').html("<%= j (render 'comments/form') %>");
$('.commentbox').focus();
$('.commentbox').val('.commentbox').val("<%= @comment.content %>");
...
<div class="feed-activity-list">
<div id="commentform" class="commentform">
<%= render 'comments/form' %>
</div>
<div id="commentdiv" class="commentdiv">
<%= render 'comments/comment' %>
</div>
</div>
...
$('#commentform').html("<%= escape_javascript( render 'comments/form') %>")
$('.commentbox').val('.commentbox').val('');
$('.commentbox').focus();
$('#commentdiv').html("<%= j (render 'comments/comment') %>");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment