ruby-on-railsjsonunicodedatatablesajax-request

Rails 4: converting Escaped Unicode to utf-8


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?


Solution

  • 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.