2014-02-23 1 views
0

나는 다음과 같은 커피 스크립트에 문제가없는 한 :커피 스크립트가 완료되지 있지만 오류

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) 

는 기본적으로, 나는에 주석을 추가 할 사용자 양식을 가지고있다. 새로운 설명이 추가되면 데이터를 데이터베이스로 보내기 전에 클래스를 변경하고 텍스트 영역을 비활성화해야합니다. 그런 다음 클래스를 재설정하고 텍스트 영역을 지우고 텍스트 영역을 다시 활성화해야합니다. 그런 다음 마침내 텍스트 영역 다음에 새로운 주석을 추가해야합니다.

코드의 첫 번째 부분이 작동하고 클래스가 텍스트 영역에 추가되고 비활성화 됨으로 설정되지만 나머지 스크립트는 절대로 발생하지 않습니다. 물론 주석은 실제로 데이터베이스에 저장되고 페이지 새로 고침은 주석을 표시합니다.

나는이 시간이 넘었으며 무엇이 잘못 될지 알 수 없습니다. 나는 들여 쓰기가 스크립트에 잘못되어있는 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> 

, 삭제 동작이 부분적으로 작동합니다. 데이터베이스에서 주석을 삭제하고 주석을 모두 숨 깁니다. 페이지 새로 고침은 삭제되지 않은 주석을 표시합니다.

아무 도움이되지 않을 것이기 때문에 도움이 될 것입니다. 내 마음에 그것은 작동해야합니다 그래서 내가 뭔가를 놓치고 있어야합니다 간단합니다.

+0

왜 커피 코드에 세미콜론이 있습니까? 최신 Coffeescript에서는 훨씬 더 관용적 인 코드를 작성할 수 있습니다. https://gist.github.com/elclanrs/077b1271014d1a039afb. – elclanrs

+1

또한, 나는 당신이 당신의 신원에 관한 문제를 가지고 있을지도 모른다고 생각합니다. 그것은 무엇입니까? ""아약스 : 성공 "바인딩? 요점에 대한 나의 편집을 보아라. – elclanrs

+0

그래, 그 것이었다. 들여 쓰기가 완전히 누락되었습니다. 이 coffeescript 놈을 도와 주셔서 감사합니다. –

답변

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') 
관련 문제