简体   繁体   English

Rails从2.3升级到3.2 javascript呈现为文本

[英]Rails upgrade from 2.3 to 3.2 javascript rendering as text

Recently I am upgrading one of the rails app from 2.3 version to 3.2 version. 最近我将其中一个rails应用程序从2.3版本升级到3.2版本。 And I am noticing a strange behavior in the javascript written in the html.erb files. 我注意到在html.erb文件中编写的javascript中的一个奇怪的行为。 This never happened before with other apps I have upgraded. 这与我升级的其他应用之前从未发生过。

I am upgrading to Ruby 1.9.3 Rails 3.2.14 我正在升级到Ruby 1.9.3 Rails 3.2.14

Currently jquery version is 1.4 and I would upgrade it 1.9 or 1.10 once the application is fully ported. 目前jquery版本是1.4,一旦应用程序完全移植,我会将它升级到1.9或1.10。

Here is the problem. 这是问题所在。 Whenever we have some JS written in templates and this JS has greater then condition at some point then any JS code written after that is rendered as text on the page without any JS errors in firebug. 每当我们在模板中编写一些JS并且这个JS在某些时候具有更大的条件,那么之后编写的任何JS代码在页面上呈现为文本而在firebug中没有任何JS错误。 I have attached the screen-shot please have a look. 我已经附上了屏幕截图请看看。

And when I remove that JS code into a .js file then this problem disappears. 当我将该JS代码删除到.js文件中时,此问题就会消失。 I think is something has to ERB template parsing as the same code working fine in the rails 2.3 application. 我认为有必要将ERB模板解析为在rails 2.3应用程序中正常工作的相同代码。

在此输入图像描述

In the screen shot the JS code rendered something like 在屏幕截图中,JS代码呈现了类似的东西

" 0) jQuery("#li_" + caab_jurisdiction_name).show();..." 

it starts wth something like this 它开始像这样的东西

"if (jQuery("#mortgage_product_group_econveyance_editions").attr("value").indexOf(caab_jurisdiction_value) >  0) jQuery("#li_" + caab_jurisdiction_name).show();..."

Rails 3.0+ has XSS protection by default, whereas in Rails 2.3 you need to use the helper h in order of use it. Rails 3.0+默认具有XSS保护,而在Rails 2.3中,您需要按顺序使用帮助程序h。

To avoid it, you can use String#html_safe or the helper raw. 为避免这种情况,您可以使用String#html_safe或helper raw。

You can read more about it in this railscasts http://railscasts.com/episodes/204-xss-protection-in-rails-3 . 您可以在此railscast中了解更多相关内容http://railscasts.com/episodes/204-xss-protection-in-rails-3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM