Rails3 renders a js.erb template with a text/html content-type instead of text/javascript Rails3 renders a js.erb template with a text/html content-type instead of text/javascript ajax ajax

Rails3 renders a js.erb template with a text/html content-type instead of text/javascript


I finally was able to get the right content-type in the response by forcing it with:

respond_to do |format|    format.js {render :content_type => 'text/javascript'}end


With Rails 3.1.1 I ran into a similar issue. My response would render as html even though I had explicitly mentioned format.js in my controller. It turns out the problem was that I had jquery.js files in my assets and being loaded by the template. jquery-rails uses its own copy of jquery and having these files also loaded caused rails to render the response as html for some reason. Might help someone else stuck with this issue.


had a similar problem where the js file would load as a text file, but in the end it was just my scrits which were loading in the wrong order.

replace

<%= javascript_include_tag :all %>

with

<%= javascript_include_tag "jquery.min" %><%= javascript_include_tag "jquery-ui.min" %><%= javascript_include_tag "rails" %>

Also to note, the getscript method

ie replace:

onClick="<%= get_pubmed_data_path(:format => :js) %>"

with:

onClick="$.getScript('<%= escape_javascript(get_pubmed_data_path(:format => :js)) %>');"