找不到xhr未定义的原因

时间:2014-02-22 17:36:16

标签: javascript ruby-on-rails coffeescript

我收到了一个未被捕获的引用错误:XHR未在下面的coffeescript中定义。

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)

我一直无法弄清楚这个问题,我在javascript中相当弱。

我要做的是在用户页面添加注释,然后通过AJAX显示。注释保存没有任何问题,因为我可以看到它,如果我手动刷新页面。但是,创建或删除操作都不适用于Coffeescript。

由于创建或删除AJAX调用似乎都不起作用,我假设它是调用脚本的方式。我也会在这里包含相关的控制器代码。

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

也是我评论的部分内容:

 <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>

表单本身添加新注释:

<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>

任何帮助都会受到赞赏,因为我现在还不知道从哪里开始。我不确定如何定义XHR。

顺便提一下,大部分代码来自教程here

0 个答案:

没有答案