Advertisement
Guest User

Untitled

a guest
Apr 16th, 2014
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.38 KB | None | 0 0
  1. jQuery ->
  2. # Create a comment
  3. $(".comment-form")
  4. .on "ajax:beforeSend", (evt, xhr, settings) ->
  5. $(this).find('textarea')
  6. .addClass('uneditable-input')
  7. .attr('disabled', 'disabled');
  8. .on "ajax:success", (evt, data, status, xhr) ->
  9. $(this).find('textarea')
  10. .removeClass('uneditable-input')
  11. .removeAttr('disabled', 'disabled')
  12. .val('');
  13. $(xhr.responseText).hide().insertAfter($(this)).show('slow')
  14.  
  15. # Delete a comment
  16. $(document)
  17. .on "ajax:beforeSend", ".comment", ->
  18. $(this).fadeTo('fast', 0.5)
  19. .on "ajax:success", ".comment", ->
  20. $(this).hide('fast')
  21. .on "ajax:error", ".comment", ->
  22. $(this).fadeTo('fast', 1)
  23.  
  24. class CommentsController < ApplicationController
  25. before_action :set_comment, only: [:show, :destroy]
  26.  
  27. def create
  28. @comment_hash = comment_params
  29. @obj = @comment_hash[:commentable_type].constantize.find(@comment_hash[:commentable_id])
  30. # Not implemented: check to see whether the user has permission to create a comment on this object
  31. @comment = Comment.build_from(@obj, current_user, @comment_hash[:body])
  32. @comment.user = current_user
  33. if @comment.save
  34. render partial: "comments/comment", locals: { comment: @comment }, layout: false, status: :created
  35. else
  36. p @comment.errors
  37. render js: "alert('error saving comment');"
  38. end
  39. end
  40.  
  41. def destroy
  42. if @comment.destroy
  43. render json: @comment, status: :ok
  44. else
  45. render js: "alert('error deleting comment');"
  46. end
  47. end
  48.  
  49. private
  50.  
  51. # Use callbacks to share common setup or constraints between actions.
  52. def set_comment
  53. @comment = Comment.find(params[:id])
  54. end
  55.  
  56. # Never trust parameters from the scary internet, only allow the white list through.
  57. def comment_params
  58. params.require(:comment).permit( :commentable_id, :commentable_type, :body, :user_id)
  59. end
  60.  
  61. end
  62.  
  63. <div class='comment-form'>
  64. <%= simple_form_for comment, remote: true do |f| %>
  65. <%= f.input :body, input_html: { rows: "2" }, label: false %>
  66. <%= f.input :commentable_id, as: :hidden, value: comment.commentable_id %>
  67. <%= f.input :commentable_type, as: :hidden, value: comment.commentable_type %>
  68. <%= f.button :submit, 'Save Note', class: "button tiny radius", disable_with: "Submitting…" %>
  69. <% end %>
  70. </div>
  71.  
  72. <div class='comment'>
  73. <hr>
  74. <%=link_to "×", comment_path(comment), method: :delete, remote: true, data: { confirm: 'Are you sure you want to remove this comment?',disable_with: 'x' }, class: 'close' %>
  75. <small><%=comment.updated_at.to_s(:short) %></small>
  76. <p><%= comment.body %></p>
  77.  
  78. jQuery ->
  79. # Create a comment
  80. $(".comment-form")
  81. .on "ajax:beforeSend", (evt, xhr, settings) ->
  82. $(this).find('textarea')
  83. .addClass('uneditable-input')
  84. .attr('disabled', 'disabled');
  85. .on "ajax:success", (evt, data, status, xhr) -> #<-- this line!
  86. $(this).find('textarea')
  87. .removeClass('uneditable-input')
  88. .removeAttr('disabled', 'disabled')
  89. .val('');
  90. $(xhr.responseText).hide().insertAfter($(this)).show('slow')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement