form_tag not passing a js call in Rails3

This seems like it should be pretty straightforward, but its stumping me. I have a small form that I am trying to submit using jquery that is supposed to remove an association between to HABTM models. For the sake of argument, the models are Course and Student.

# view      <%= form_tag ( remove_student_from_course_path, :remote => true ) %>         <%= hidden_field_tag "course_id", %>         <%= hidden_field_tag "student_id", %>      <%= submit_tag 'Remove' %>

# controller   def remove_student_from_class     @student = Student.find(params[:student_id])     @msg = Factor.method_to_remove_student_from_class(params)

    respond_to do |format|         format.html { redirect_to edit_class_path(@class) }         format.js     end   end

# remove_student_from_class.js.erb // reload the page location.reload();

Everything works fine, except the HTML code is generated and not the JS code in the respond_to block. I would expect the JS call to get fired since I have the ':remote => true' line in there, but its always the HTML and it generates the page. What am I overlooking?


Are you including rails.js?

Yes I am, here's what I have in the application.html.erb view, and they are all in my javascript folder

<%= javascript_include_tag 'jquery-1.4.3.js', 'rails', 'application' %>

Yes I am, here's what I have in the application.html.erb view, and they are all in my javascript folder

<%= javascript_include_tag 'jquery-1.4.3.js', 'rails', 'application' %>

Ah, but have you replaced rails.js with the jquery version?

curl -O

see the README at for more details.


Yep, i've done that, too. Here's the header of the 'rails.js' version i'm using

/* * jquery-ujs

Do you have anything pertinent/related/involved in the application.js file? Try pulling that out as you could be having a failure in JS that's causing grief. Also -- check your console in Firebug (or whatever you're using) and see if errors are being thrown when the page loads.

Nope, nothing in the application.js file. Checked the firebug output and there is nothing. The JS is not even getting fired, which make no sense because I have the ':remote => true' statement in there. It even creates the 'data-remote' call in the HTML output (see below). I thought that was the key to UJS in Rails3?

<form accept-charset="UTF-8" action="/course/ remove_student_from_course" data-remote="true" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="&#x2713;" /><input name="authenticity_token" type="hidden" value="4uzcKHN73cndlQENeb7IWRhe6g0F5eFZG91y3ACWO0M=" /></

        <input id="course_id" name="course_id" type="hidden" value="1529" />         <input id="student_id" name="student_id" type="hidden" value="437" />         <input class="submitLink" name="commit" type="submit" value="Remove" />       </form>

I'm sure you checked this but just in case... based on what you just said, check to make sure you haven't disabled JS in your browser. Put a "Hello World" script in Application.js

I'm using jQuery in Rails 3 with Ruby 1.9.2 and it's working fine:

I use <%= javascript_include_tag :default %> with :default defined as

    config.action_view.javascript_expansions[:defaults] = %w(jquery.min rails jquery-ui.min)

in my application.rb file.

HTH, Dan

Yep, Javascript is enabled, and working fine. Apparently, I have stumped the brightest minds in the rails world :slight_smile: That can only mean one thing...i'm overlooking something very simple and I'll feel quite dumb when the answer is presented to me :slight_smile:

Show is what is in the log when you do the submit.


The problem appears to be the dataType of the Ajax request -- it is requesting an HTML response rather than a script. I wonder if the default has changed recently?

Anyway, calling

     $.ajaxSetup({ dataType: 'script' });

at the top of your application.js or similar will change the default and appears to restore the expected behaviour. You could also set the data-type="script" attribute on your <form> or <a> tags.

I'm having exactly the same issues as the original poster, and have checked all the settings mentioned above, and it's still not working.

I tried adding the $.ajaxSetup({ dataType: 'script' }); to my application.js, and data-type="script" to my form, and it didn't help.

Any other ideas? Thanks...

I found the answer (at least for me)!

The issue is described here:

More info here on the official fix:

The solution was to add a line of jQuery to set the proper request header to handle Ajax calls. In my app/views/layouts/application.html.erb, just add this after you include the jQuery file:

<script type="text/javascript">   jQuery.ajaxSetup({     beforeSend: function (xhr) {       xhr.setRequestHeader("Accept", "text/javascript");     }   }); </script>

Are you using rails.js?