Note that there are some explanatory texts on larger screens.

plurals
  1. PORails 3 prototype render :update response has text/html content-type
    text
    copied!<p>I'm trying to get some RJS code written with prototype and Rails 2.3.11 to work in Rails 3.2.1</p> <p>I have the <code>prototype-rails</code> gem, so <code>render :update do |page|</code> works, I'm using a form with <code>:remote =&gt; true</code> that sends an ajax request to the controller, and the javascript looks like it's being generated ok. </p> <p>However, the content-type in the header of the response is <code>text/html; charset=utf-8</code>, which should be <code>text/javascript</code>.</p> <p>In the controller I'm calling it like this:</p> <pre><code>render :update do |page| if @step.errors.empty? page.redirect_to how_to_path(@article.id) else page.replace_html 'add_step_form', :partial =&gt; 'how_to/add_step', :locals =&gt; {:step =&gt; @step, :altered =&gt; true} end end </code></pre> <p>It seems to generate the <code>window.location.href...</code> and <code>Element.update...</code> code ok, but it isn't executing because the content-type is wrong.</p> <p>Is there something that I might be doing wrong that could cause this? I need a solution that will make rjs with prototype work. jQuery will probably be used in the future, but making that change right now isn't an option.</p> <p><strong>update:</strong> I've tried a few other ways of writing the code, including specifying <code>:content_type =&gt; "text/javascript"</code> in <code>render</code>, wrapping it in a <code>respond_to</code> block with <code>format.js</code>, and rewriting it as <code>js.erb</code> file, but all still are returning with <code>text/html</code> as the content-type in the response header.</p> <p><strong>update</strong> I sort of figured out how to get the expected behavior by adding <code>headers["Content-Type"] = "text/javascript; charset=utf-8"</code> in the controller before <code>render</code>, but this doesn't really seem like the best way to do it if I have to add that explicitly before every RJS instance. I'd like a cleaner solution if anyone can come up with one.</p> <p><strong>update</strong> It turns out we had a <code>before_filter</code> running before every request that was setting the content-type to text/html. I removed this, and was able to remove all of the <code>headers["Content-Type"]</code> code that I added. It worked in my development environment but not in our testing verification environment. That turned out to be we had old assets cached on there, so verification was running prototype 1.6.1, while my local development environment had 1.7.0. That caused <code>rails.js</code> not to compile in verification, so all the requests had an <code>Accepts: text/html</code> instead of <code>text/javascript</code>. Flushing that cache loaded the newer version of prototype and fixed the problem. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload