Rails 3原型渲染:更新响应具有text / html内容类型 [英] Rails 3 prototype render :update response has text/html content-type

查看:95
本文介绍了Rails 3原型渲染:更新响应具有text / html内容类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用原型和Rails 2.3.11编写一些RJS代码以在Rails 3.2.1中工作

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

我有 prototype-rails gem,所以 render:update do | page | 可以工作,我使用的是的表单:远程=> true ,它将ajax请求发送到控制器,而javascript看起来就可以正常生成了。

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.

但是,响应标题中的content-type是 text / html; charset = utf-8 ,应为 text / javascript

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

控制器我这样称呼它:

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

似乎会生成 window.location.href ... Element.update ... 代码正常,但由于内容-类型是错误的。

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

我是否做错了某些事情,可能会导致这种情况?我需要一个可以使rjs与原型一起工作的解决方案。将来可能会使用jQuery,但现在不能进行更改。

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.

更新:
我已经尝试了其他几种编写代码的方法,包括指定:content_type =>。 渲染中,将其包装在 respond_to 块中,并带有 format.js ,并将其重写为 js.erb 文件,但所有文件仍以 text返回/ html 作为响应标题中的内容类型。

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.

更新
通过在<$ c $之前的控制器中添加 headers [ Content-Type] = text / javascript; charset = utf-8 来了解如何获得预期的行为c> render
,但是如果我必须在每个RJS实例之前显式地添加它,这似乎并不是最好的方法。我希望有一个更干净的解决方案,如果有人可以提出。

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.

更新
事实证明我们有一个 before_filter 在将内容类型设置为text / html的每个请求之前运行。我删除了它,并且能够删除我添加的所有 headers [ Content-Type] 代码。它在我的开发环境中有效,但在我们的测试验证环境中无效。原来是我们在那里缓存了旧资产,所以验证运行的是1.6.1原型,而我的本地开发环境是1.7.0。这导致 rails.js 无法在验证中编译,因此所有请求都具有 Accepts:text / html 而不是文本/ javascript 。刷新该缓存会加载较新版本的原型,并解决了该问题。

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.

推荐答案

事实证明,在每个将内容类型设置为text / html的请求之前,我们都有一个before_filter在运行。我删除了它,并且它没有下面的hack。

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.

我唯一想做的就是添加 headers [ Content-Type] = text / javascript; charset = utf-8 render之前:更新

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

不幸的是,我不得不添加它在代码中调用RJS render:update 的每个位置。

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

这篇关于Rails 3原型渲染:更新响应具有text / html内容类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆