title: Coffee on Enter Keypress Repeatedly Sends PUT Request
body: <p>I have this coffeescript code that accepts an "Enter" keypress in order to submit a form through ajax</p>
  
  <pre><code>$(".text_field.comment").keypress (e) -&gt;
    if e.which is 13    
      $(this).blur()
      form = $(this).closest("form")
      $.ajax
        url: form.attr('action')
        type: "PUT"
        dataType: "json"
        data: form.serialize()
      false
  </code></pre>
  
  <p>What happens here is that it repeatedly sends the request for almost 20 times!!! What must be done to stop repetitive sending of request?</p>
  
  <p>Stupid me! The answer lies in my controller. Before I have this:</p>
  
  <pre><code>class AnswersController &lt; ApplicationController
    before_filter :authenticate_user!
  
    def update
      @answer = Answer.find(params[:id])
      if @answer.update_attributes(params[:answer])
        redirect_to(@answer,
          :notice =&gt; I18n.t('answer.notice'))
      else
        flash[:error] = @answer.errors.full_messages.to_sentence
        redirect_to @answer
      end 
    end 
  end
  </code></pre>
  
  <p>Then I changed it to this:</p>
  
  <pre><code>class AnswersController &lt; ApplicationController
    before_filter :authenticate_user!
  
    def update
      @answer = Answer.find(params[:id])
  
      respond_to do |format|
        if @answer.update_attributes(params[:answer])
          format.json { render :json =&gt; @answer }
        else
          format.json { render :json =&gt; @answer.errors.full_messages.to_sentence } #output javascript messages
        end
      end
    end
  end
  </code></pre>
  
  <p>The reason for the repeated requests is because it's having a circular request of "GET /answers/1"</p>