respond_to not working with format.js if format.html is present

1.3k views Asked by At

When using respond_to, my AJAX call only invokes the javascript response if the HTML response isn't present.

I have the following javascript for submitting an edit-in-place action:

$(".eip").editable("/surveys", {
  name   : 'survey[name]',
  select : true,
  event  : 'edit',
  submit : 'Save',
  cancel : 'cancel',
  cssclass : "editable",
  onblur : 'ignore',
  onedit : function() { $(".eip_desc").hide(); },
  onreset : function() { $(".eip_desc").show(); },
  onsubmit : function() { $(".eip_desc").show(); }
});

Then I have the following Survey controller method:

class SurveysController < ApplicationController
  def create
    @survey = Survey.new(params[:survey])    

    if @survey.save
      respond_to do |format|
        format.html { redirect_to surveys_path, :notice => "Survey created!" }
        format.js
      end
    else
      render :action => :new
    end
  end
end

If I remove the format.html line, then format.js responds as it should. But if I leave format.html in there, then the whole page renders when I submit via that editable javascript bit.

I'm running Rails 3.0.3 for this app.

1

There are 1 answers

1
andrea On

from docs

(String) method: Method to use when submitting edited content. Default is POST. You most likely want to use POST or PUT. PUT method is compatible with Rails.

try to pass a method: "put"