Can't find reason why xhr is not defined

2019-08-27 19:22发布

问题:

I am getting an uncaught reference error: XHR is not defined in my coffeescript below.

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)

I have been unable to figure out the issue and I've pretty weak in javascript.

What I'm trying to do is add a comment to a users page then show it via AJAX. The comment saves without any problem as I can see it if I manually refresh the page. However neither the create or delete actions work in the Coffeescript.

Since neither the create or delete AJAX calls seem to work, I am assuming it's in the way the script is called. I'll include the relevant controller code here as well.

class CommentsController < ApplicationController
  before_action :set_comment, only: [:show, :destroy]

  def create
    @comment_hash = comment_params
    @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])
    @comment.user = current_user
    if @comment.save
      render partial: "comments/comment", locals: { comment: @comment }, layout: false, status: :created
    else
      p @comment.errors
      render js: "alert('error saving comment');"
    end
  end

  def destroy
    if @comment.destroy
      render json: @comment, status: :ok
    else
      render js: "alert('error deleting comment');"
    end
  end

  private

# Use callbacks to share common setup or constraints between actions.
def set_comment
  @comment = Comment.find(params[:id])
end

# Never trust parameters from the scary internet, only allow the white list through.
def comment_params
  params.require(:comment).permit( :commentable_id, :commentable_type, :body, :user_id)
end

end

Also my partial for the comment:

 <div class='comment'>
      <hr>
      <%=link_to "×", comment_path(comment), method: :delete, remote: true, confirm: "Are you sure you want to remove this comment?", disable_with: "×", class: 'close' %>
        <small><%=comment.updated_at.to_s(:short) %></small>
      <p><%= comment.body %></p>

And the form itself to add new comments:

<div class='comment-form'>
    <%= simple_form_for comment, remote: true do |f| %>
    <%= f.input :body, input_html: { rows: "2" }, label: false %>
    <%= f.input :commentable_id, as: :hidden, value: comment.commentable_id %>
    <%= f.input :commentable_type, as: :hidden, value: comment.commentable_type %>
    <%= f.button :submit, 'New Note', class: "button tiny radius", disable_with: "Submitting…" %>
    <% end %>
</div>

Any help would be appreciated since I just don't know where to start right now. I'm not sure how I should be defining the XHR.

Incidentally, most of the code for this was from the tutorial here