简体   繁体   English

如何在Ruby on Rails项目中使用JavaScript解码HTML实体

[英]How to decode HTML entities in JavaScript using in a Ruby on Rails project

I am dealing with Ruby on Rails and JavaScript. 我正在处理Ruby on Rails和JavaScript。
The main basic idea is that I create an object in my model's class function and call this function in JavaScript. 基本的基本思想是,我在模型的类函数中创建一个对象,然后在JavaScript中调用此函数。 The problem there is that anywhere the HTML entities are encoded . 那里的问题HTML实体被编码的任何地方。

Now in detail: 现在详细介绍:
In my model's function getData(firstOpt, secondOpt, amount) I create an object that looks as follows (when I call the function in the ruby console): 在模型的函数getData(firstOpt, secondOpt, amount) ,创建一个如下所示的对象(当我在ruby控制台中调用该函数时):

[#<JobCompact build_compact_id: 324>, #<JobCompact build_compact_id: 325>, #<JobCompact build_compact_id: 325>] 

Now I put this into an array (because I want to have more entries in that array later) at the first position: 现在,我将其放在第一个位置的数组中(因为稍后我想在该数组中添加更多条目):

data[0] = firstLang  

(note: I also tried -> data[0] = firstLang.to_json) (注意:我也尝试过-> data [0] = firstLang.to_json)

In the end I just return the array data. 最后,我只返回数组数据。 Now I call the function in JavaScript: 现在,我在JavaScript中调用该函数:

var curData = <%= BuildCompact.getData("ruby", "rubinius", 15) %>;

And the error is: Uncaught SyntaxError: Unexpected token ILLEGAL 错误是: Uncaught SyntaxError: Unexpected token ILLEGAL
because it translated my result of the ruby function to: var curData = [[#&lt;JobCompact id: 841, language: &quot;ruby&quot;, version: &quot;1.609481891837258&quot; 因为它将我的ruby函数的结果转换为:var curData = [[#&lt;JobCompact id: 841, language: &quot;ruby&quot;, version: &quot;1.609481891837258&quot; etc. 等等

I also tried to decode the result: 我也尝试解码结果:
var curData = htmlentities((<%= BuildCompact.getData("ruby", "rubinius", 15) %>).toString());

And many other variants of that but I cannot figure it out. 还有其他许多变体,但我无法弄清楚。 Has anybody had a similar problem? 有人有类似的问题吗?

NOTE: I hope that post is NOT a duplicate because I do not know where to encode (in Ruby oder Javascript). 注意:我希望该帖子不会重复,因为我不知道在哪里编码(在Ruby oder Javascript中)。

好的,我的问题的答案是:

var curData = <%=raw BuildCompact.getData("ruby", "rubinius", 500).to_json %>;

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

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