[英]Rails 3 prototype render :update response has text/html content-type
I'm trying to get some RJS code written with prototype and Rails 2.3.11 to work in Rails 3.2.1 我正在尝试使用原型和Rails 2.3.11编写的一些RJS代码在Rails 3.2.1中工作
I have the prototype-rails
gem, so render :update do |page|
我有
prototype-rails
gem,所以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. 工作,我正在使用一个表单
:remote => true
,它向控制器发送一个ajax请求,并且javascript看起来好像正在生成。
However, the content-type in the header of the response is text/html; charset=utf-8
但是,响应标题中的content-type是
text/html; charset=utf-8
text/html; charset=utf-8
, which should be text/javascript
. text/html; charset=utf-8
,应该是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. 它似乎生成
window.location.href...
和Element.update...
代码确定,但它没有执行,因为内容类型是错误的。
Is there something that I might be doing wrong that could cause this? 我可能做错了会导致这种情况吗? I need a solution that will make rjs with prototype work.
我需要一个能让原型工作的rjs的解决方案。 jQuery will probably be used in the future, but making that change right now isn't an option.
jQuery可能会在未来使用,但现在进行更改不是一种选择。
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. 更新:我已经尝试了其他一些编写代码的方法,包括在
render
指定:content_type => "text/javascript"
,将其包装在带有format.js
的respond_to
块中,并将其重写为js.erb
文件,但是所有仍然返回text/html
作为响应头中的内容类型。
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. 更新我有点弄清楚如何通过在
render
之前在控制器中添加headers["Content-Type"] = "text/javascript; charset=utf-8"
来获得预期的行为,但这看起来并不是最好的如果我必须在每个RJS实例之前明确添加它,那么这样做的方法。 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. 更新事实证明,在将content-type设置为text / html的每个请求之前,我们运行了一个
before_filter
。 I removed this, and was able to remove all of the headers["Content-Type"]
code that I added. 我删除了这个,并且能够删除我添加的所有
headers["Content-Type"]
代码。 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.
结果是我们在那里缓存了旧资产,因此验证运行原型1.6.1,而我的本地开发环境有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
. 这导致
rails.js
无法在验证中编译,因此所有请求都有一个Accepts: text/html
而不是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. 事实证明,我们在每个将content-type设置为text / html的请求之前运行了一个before_filter。 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
我唯一想要做的就是在
render :update
之前添加headers["Content-Type"] = "text/javascript; charset=utf-8"
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. 不幸的是,我必须在RJS
render :update
的代码中的每个地方添加它render :update
被调用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.