Rails 3 原型渲染:更新响应具有 text/html 内容类型
我试图让一些用原型和 Rails 2.3.11 编写的 RJS 代码在 Rails 3.2.1 中工作
我有 prototype-rails
gem,所以 render :update do |page|
有效,我正在使用带有 :remote => 的表单true
向控制器发送 ajax 请求,并且 javascript 看起来生成正常。
然而,响应头中的内容类型是text/html; charset=utf-8
,应该是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...
代码,但它由于内容类型错误而未执行。
我是否做错了什么可能会导致这种情况?我需要一个能够使 rjs 与原型一起工作的解决方案。将来可能会使用 jQuery,但现在无法进行更改。
更新: 我尝试了一些其他编写代码的方法,包括指定 :content_type =>
,使用 render
中的“text/javascript”format.js
将其包装在 respond_to
块中,并将其重写为 js.erb
文件,但所有内容仍然以 text/html
作为响应标头中的内容类型返回。
更新 我想出了如何通过在渲染之前在控制器中添加 headers["Content-Type"] = "text/javascript; charset=utf-8" 来获得预期的行为code>,但是如果我必须在每个 RJS 实例之前显式添加它,那么这似乎并不是最好的方法。如果有人能想出一个更干净的解决方案,我想要一个。
更新 事实证明,我们在每个将内容类型设置为 text/html 的请求之前运行了一个 before_filter
。我删除了它,并且能够删除我添加的所有 headers["Content-Type"]
代码。它在我的开发环境中有效,但在我们的测试验证环境中无效。事实证明,我们在那里缓存了旧资源,因此验证运行的是原型 1.6.1,而我的本地开发环境运行的是 1.7.0。这导致 rails.js
在验证中无法编译,因此所有请求都有一个 Accepts: text/html
而不是 text/javascript
。刷新缓存加载了新版本的原型并解决了问题。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
事实证明,我们有一个 before_filter 在每个将内容类型设置为 text/html 的请求之前运行。我删除了它,并且在没有下面的 hack 的情况下它也能工作。
但如果您需要解决方法,这就是我在下面所做的。
我唯一想出的办法是在
render :update
之前添加headers["Content-Type"] = "text/javascript; charset=utf-8"
code>不幸的是,我必须将它添加到代码中调用 RJS
render :update
的每个位置。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 therender :update
Unfortunately, I've had to add it in every place in the code where the RJS
render :update
is called.