Created
May 5, 2018 12:22
-
-
Save spacerobotTR/d77d38ced49224ae11d80ed09aa4ed8b to your computer and use it in GitHub Desktop.
Rails Ajax Comments
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<% @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 %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%= 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 %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$('#commentform').html("<%= j (render 'comments/form') %>"); | |
$('.commentbox').focus(); | |
$('.commentbox').val('.commentbox').val("<%= @comment.content %>"); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
<div class="feed-activity-list"> | |
<div id="commentform" class="commentform"> | |
<%= render 'comments/form' %> | |
</div> | |
<div id="commentdiv" class="commentdiv"> | |
<%= render 'comments/comment' %> | |
</div> | |
</div> | |
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$('#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