Coffeescript没有完成但没有错误

时间:2014-02-23 01:13:05

标签: javascript jquery ruby-on-rails ruby coffeescript

我在使用以下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)

基本上,我有一个用户表单,我添加了评论。添加新注释时,它应更改类并在将数据发送到数据库之前禁用textarea。然后它应该重置类,清除textarea并再次启用textarea。最后它应该在textarea之后添加新的评论。

代码的第一部分工作,类被添加到textarea,它被设置为禁用但脚本的其余部分永远不会发生。当然,评论实际上已保存到数据库中,刷新页面将显示评论。

我已经过了很多次,无法弄清楚出了什么问题。 earlier issue,但是脚本的缩进是错误的,但是已经修复了。

我的CommentController代码如下:

    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-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, 'Save Note', class: "button tiny radius", disable_with: "Submitting…" %>
    <% end %>
</div>

这是我显示评论的代码:

 <div class='comment'>
        <hr>
        <%=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' %>
        <small><%=comment.updated_at.to_s(:short) %></small>
        <p><%= comment.body %></p>

顺便说一句,删除操作部分有效。它从数据库中删除注释,并隐藏所有注释。页面刷新显示尚未删除的注释。

任何帮助都会受到高度赞赏,因为我根本不知道该去哪里。在我看来它应该工作所以我必须遗漏一些简单的东西。

1 个答案:

答案 0 :(得分:1)

这是你缩进的问题。行.on "ajax:success"必须缩进到与其他.on相同的级别。并且后面的代码也必须相应缩进:

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 line!
            $(this).find('textarea')
                .removeClass('uneditable-input')
                .removeAttr('disabled', 'disabled')
                .val('');
            $(xhr.responseText).hide().insertAfter($(this)).show('slow')