title: How to make a Jquery Ajax request hit a Rails Controllers json format?
body: <p>I have a Jquery ajax request which I'm using to UPDATE a record.  I'm trying to get back the JSON object to use in the success/error callback but the object <code>msg</code> is always the full rendered html.  How can I have this request hit the <code>format.json</code> in the users#update method?</p>
  
  <p><strong>edit.html.erb</strong></p>
  
  <pre><code>&lt;script&gt;
      // when the #email_field_(id) field changes
      var email_timeout;
      $("#email_field_&lt;%= @user.id %&gt;").keypress(function() {
          clearTimeout(email_timeout);
          email_timeout = setTimeout(function(){ 
              $("#email_field_&lt;%= @user.id %&gt;").attr("disabled", "disabled");
              $.ajax({
                  type: "POST",
                  callback: "JSON",
                  url: '&lt;%= user_path(@user.id) %&gt;',
                  data: { _method:'PUT', user: { email : $("#email_field_&lt;%= @user.id %&gt;").val() } },
                  success: function(msg) {
                      console.log(msg); //Always full rendered html
                      $("#email_field_&lt;%= @user.id %&gt;").removeAttr("disabled");
                      $("#email_field_&lt;%= @user.id %&gt;").val(msg.email);
                  },
                  error: function(msg) {
                      $("#email_field_&lt;%= @user.id %&gt;").removeAttr("disabled");
                      $("#email_field_&lt;%= @user.id %&gt;").val("&lt;%= @user.email %&gt;");
                      alert( "Ajax error!" );
                  }
              });
          },1000);
      });
  &lt;/script&gt;
  </code></pre>
  
  <p><strong>users_controller.rb</strong></p>
  
  <pre><code># PATCH/PUT /users/1
  # PATCH/PUT /users/1.json
  def update
    respond_to do |format|
      if @user.update(user_params)
        puts '****: ' + format.inspect
        format.html { redirect_to @user, notice: 'User was successfully updated.' }
        format.json { render :show, status: :ok, location: @user }
      else
        format.html { render :edit }
        format.json { render :json =&gt; { :error =&gt; "Error: unable to save data" }.to_json, :status =&gt; 422 }
      end
    end
  end
  </code></pre>