質問

I'm trying to get some RJS code written with prototype and Rails 2.3.11 to work in Rails 3.2.1

I have the prototype-rails gem, so render :update do |page| works, I'm using a form with :remote => true that sends an ajax request to the controller, and the javascript looks like it's being generated ok.

However, the content-type in the header of the response is text/html; charset=utf-8, which should be text/javascript.

In the controller I'm calling it like this:

render :update do |page|
    if @step.errors.empty?
        page.redirect_to how_to_path(@article.id)
    else
        page.replace_html 'add_step_form', :partial => 'how_to/add_step', :locals => {:step => @step, :altered => true}
    end
end

It seems to generate the window.location.href... and Element.update... code ok, but it isn't executing because the content-type is wrong.

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.

update: I've tried a few other ways of writing the code, including specifying :content_type => "text/javascript" in render, wrapping it in a respond_to block with format.js, and rewriting it as js.erb file, but all still are returning with text/html as the content-type in the response header.

update I sort of figured out how to get the expected behavior by adding headers["Content-Type"] = "text/javascript; charset=utf-8" in the controller before render, 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.

update It turns out we had a before_filter running before every request that was setting the content-type to text/html. I removed this, and was able to remove all of the headers["Content-Type"] 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 rails.js not to compile in verification, so all the requests had an Accepts: text/html instead of text/javascript. Flushing that cache loaded the newer version of prototype and fixed the problem.

役に立ちましたか?

解決

It turns out that we had a before_filter that was being run before every request setting the content-type to text/html. I removed that, and it worked without the hack below.

But if you need a workaround, here's what I did below.

The only thing I figure out to make this work was to add headers["Content-Type"] = "text/javascript; charset=utf-8" before the render :update

headers["Content-Type"] = "text/javascript; charset=utf-8"
render :update do |page|
  if @step.errors.empty?
    page.redirect_to how_to_path(@article.id)
  else
    page.replace_html 'add_step_form', :partial => 'how_to/add_step', :locals => {:step => @step, :altered => true}
  end
end

Unfortunately, I've had to add it in every place in the code where the RJS render :update is called.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top