简体   繁体   English

Rails 3原型渲染:更新响应具有text / html内容类型

[英]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.jsrespond_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.

相关问题 尽管有text / javascript内容类型,但仍未执行rails 3 javascript - rails 3 javascript not executed despite a text/javascript content-type in response Rails3使用text / html内容类型而不是text / javascript呈现js.erb模板 - Rails3 renders a js.erb template with a text/html content-type instead of text/javascript 在文本文件中保存响应类型“ Content-Type:text / javascript” - Save response type “Content-Type : text/javascript” in text file 'content-type':'text / html'不被接受,直到服务器被杀死 - 'content-type': 'text/html' not accepted until server is killed 如果标题中的Content-Type是text / html,则重新加载图像 - Reload image if Content-Type in header is text/html response.writeHead()方法中的{'Content-Type:':'text / plain'}的数据类型是什么? - what is data type of {'Content-Type:':'text/plain'} in response.writeHead() method? 如何从txt / html,Ubuntu,tomcat8更改响应标头内容类型 - How to change Response Header Content-Type from txt/html, Ubuntu, tomcat8 Google Drive API 响应中不需要的“content-type: text/plain;charset=UTF-8” header - Unwanted “content-type: text/plain;charset=UTF-8” header in Google Drive API Response 节点,尽管设置了application / javascript,express app仍返回text / javascript响应标头(Content-Type) - node, express app returns text/javascript response header (Content-Type) in spite of setting application/javascript 依赖Content-Type:text / plain来缓解恶意javascript执行是否安全? - Is it safe to rely on Content-Type: text/plain to mitigate malicious javascript execution in response?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM