Rails 4:将Escaped Unicode转换为utf-8(Rails 4: converting Escaped Unicode to utf-8)

我到处寻找和google / stackoverflown,但我找不到解决方案。 请帮忙!

我正在使用带有columnFilter插件的jQuery数据表来获取Ajax源表。 我正在尝试使用multiple选项实现select过滤器。

问题是这个选项包含特殊字符(中文,德文,日文,俄文等)。 现在当multiple选项关闭时。 处理json请求时没有任何问题(我得到了提交的确切字符,我使用它们来获取所需的db条目,包括所有这些语言中的条目)。 但是,当启用multiple选项时,所有特殊字符都将被转义。

基本上是选项

“简体中文(简体中文)”

当json变成了

“中国%20Simplified%20%28%u7B80%u4F53%u4E2D%u6587%29”

在后端我使用以下代码:

. . . if params[:sSearch_2].present? langs = get_items_from_multiple(params[:sSearch_2]) unless langs == [] cards = cards.where(:language => langs) end end . . . def get_items_from_multiple(request) require "cgi" itemList = request.match(/\^\((.+)\)\$/) #Extract request from within ^( )$ if itemList.nil? itemList = [] else itemList = itemList[1].split("|") #Split options escaped = [] itemList.each do |item| escaped << CGI.unescape(item).gsub("\\", "") #trying to unescape end itemList = escaped end itemList end

现在CGI.unescape...部分只是标点符号。 导致类似于:

“简体中文(%u7B80%u4F53%u4E2D%u6587)”

离开我没有转义(对不起)汉字。

我想要实现的是将一个完全未转义的字符串数组作为langs传递到此查询中:

cards = cards.where(:language => langs)

因为它们被记录在数据库中,以获取所有选定语言的项目。

我找不到ruby / rails方法,可能会这样做。 并且我不知道用前面的%代替\而不转义后者的正确方法(以便得到类似"\u7B80"东西,它无缝地变成"简" 。

有任何想法吗?

I have looked everywhere and googled / stackoverflown, but I just can't find a solution. Please help!

I am using jQuery datatables with columnFilter plugin for an Ajax-sourced table. I am trying to implement a select filter with multiple option.

The problem is that the options to this contain special characters (Chinese, German, Japanese, Russian etc.). Now when the multiple option is off. The json request is processed without any problems (I get the exact characters, that are submitted and I use them to fetch the required db entries, that include entries in all those languages). However, when the multiple option is on, all special characters get escaped.

Basically the option

"Chinese Simplified (简体中文)"

processed as json becomes

"Chinese%20Simplified%20%28%u7B80%u4F53%u4E2D%u6587%29"

In the backend I am using the following code:

. . . if params[:sSearch_2].present? langs = get_items_from_multiple(params[:sSearch_2]) unless langs == [] cards = cards.where(:language => langs) end end . . . def get_items_from_multiple(request) require "cgi" itemList = request.match(/\^\((.+)\)\$/) #Extract request from within ^( )$ if itemList.nil? itemList = [] else itemList = itemList[1].split("|") #Split options escaped = [] itemList.each do |item| escaped << CGI.unescape(item).gsub("\\", "") #trying to unescape end itemList = escaped end itemList end

Now the CGI.unescape... part only unescapes punctuation. Resulting in something like:

"Chinese Simplified (%u7B80%u4F53%u4E2D%u6587)"

Leaving me with un-unescaped (sorry for that) Chinese characters.

What I would like to achieve is pass an array of fully unescaped strings as langs into this query:

cards = cards.where(:language => langs)

as they are recorded in the db, to fetch the items in all the selected languages.

I cannot find a ruby/rails method, that might do that. And I am unaware of the proper way to substitute the preceding % with \ without escaping the latter (so as to get something like "\u7B80" which turns seamlessly into "简".

Any ideas?

最满意答案

让我们分配

str="Chinese%20Simplified%20%28%u7B80%u4F53%u4E2D%u6587%29" new_str=Rack::Utils.parse_query(URI.unescape(str))

要么

new_str=Rack::Utils.parse_query(Utils.unescape(str))

希望它能做到需要。

Lets assign

str="Chinese%20Simplified%20%28%u7B80%u4F53%u4E2D%u6587%29" new_str=Rack::Utils.parse_query(URI.unescape(str))

OR

new_str=Rack::Utils.parse_query(Utils.unescape(str))

Hope it should do the needful.

Rails 4:将Escaped Unicode转换为utf-8(Rails 4: converting Escaped Unicode to utf-8)

我到处寻找和google / stackoverflown,但我找不到解决方案。 请帮忙!

我正在使用带有columnFilter插件的jQuery数据表来获取Ajax源表。 我正在尝试使用multiple选项实现select过滤器。

问题是这个选项包含特殊字符(中文,德文,日文,俄文等)。 现在当multiple选项关闭时。 处理json请求时没有任何问题(我得到了提交的确切字符,我使用它们来获取所需的db条目,包括所有这些语言中的条目)。 但是,当启用multiple选项时,所有特殊字符都将被转义。

基本上是选项

“简体中文(简体中文)”

当json变成了

“中国%20Simplified%20%28%u7B80%u4F53%u4E2D%u6587%29”

在后端我使用以下代码:

. . . if params[:sSearch_2].present? langs = get_items_from_multiple(params[:sSearch_2]) unless langs == [] cards = cards.where(:language => langs) end end . . . def get_items_from_multiple(request) require "cgi" itemList = request.match(/\^\((.+)\)\$/) #Extract request from within ^( )$ if itemList.nil? itemList = [] else itemList = itemList[1].split("|") #Split options escaped = [] itemList.each do |item| escaped << CGI.unescape(item).gsub("\\", "") #trying to unescape end itemList = escaped end itemList end

现在CGI.unescape...部分只是标点符号。 导致类似于:

“简体中文(%u7B80%u4F53%u4E2D%u6587)”

离开我没有转义(对不起)汉字。

我想要实现的是将一个完全未转义的字符串数组作为langs传递到此查询中:

cards = cards.where(:language => langs)

因为它们被记录在数据库中,以获取所有选定语言的项目。

我找不到ruby / rails方法,可能会这样做。 并且我不知道用前面的%代替\而不转义后者的正确方法(以便得到类似"\u7B80"东西,它无缝地变成"简" 。

有任何想法吗?

I have looked everywhere and googled / stackoverflown, but I just can't find a solution. Please help!

I am using jQuery datatables with columnFilter plugin for an Ajax-sourced table. I am trying to implement a select filter with multiple option.

The problem is that the options to this contain special characters (Chinese, German, Japanese, Russian etc.). Now when the multiple option is off. The json request is processed without any problems (I get the exact characters, that are submitted and I use them to fetch the required db entries, that include entries in all those languages). However, when the multiple option is on, all special characters get escaped.

Basically the option

"Chinese Simplified (简体中文)"

processed as json becomes

"Chinese%20Simplified%20%28%u7B80%u4F53%u4E2D%u6587%29"

In the backend I am using the following code:

. . . if params[:sSearch_2].present? langs = get_items_from_multiple(params[:sSearch_2]) unless langs == [] cards = cards.where(:language => langs) end end . . . def get_items_from_multiple(request) require "cgi" itemList = request.match(/\^\((.+)\)\$/) #Extract request from within ^( )$ if itemList.nil? itemList = [] else itemList = itemList[1].split("|") #Split options escaped = [] itemList.each do |item| escaped << CGI.unescape(item).gsub("\\", "") #trying to unescape end itemList = escaped end itemList end

Now the CGI.unescape... part only unescapes punctuation. Resulting in something like:

"Chinese Simplified (%u7B80%u4F53%u4E2D%u6587)"

Leaving me with un-unescaped (sorry for that) Chinese characters.

What I would like to achieve is pass an array of fully unescaped strings as langs into this query:

cards = cards.where(:language => langs)

as they are recorded in the db, to fetch the items in all the selected languages.

I cannot find a ruby/rails method, that might do that. And I am unaware of the proper way to substitute the preceding % with \ without escaping the latter (so as to get something like "\u7B80" which turns seamlessly into "简".

Any ideas?

最满意答案

让我们分配

str="Chinese%20Simplified%20%28%u7B80%u4F53%u4E2D%u6587%29" new_str=Rack::Utils.parse_query(URI.unescape(str))

要么

new_str=Rack::Utils.parse_query(Utils.unescape(str))

希望它能做到需要。

Lets assign

str="Chinese%20Simplified%20%28%u7B80%u4F53%u4E2D%u6587%29" new_str=Rack::Utils.parse_query(URI.unescape(str))

OR

new_str=Rack::Utils.parse_query(Utils.unescape(str))

Hope it should do the needful.