从Rails的3.0.3 URL反向转义的特殊字符正确 [英] Unescape special characters correctly from the URL in Rails 3.0.3
问题描述
我用Rails 3.0.3与REE(红宝石1.8.7)和创业板mysql2,0.2.6
I'm using Rails 3.0.3 with REE ( Ruby 1.8.7 ) and gem 'mysql2', '0.2.6'
有一个在我的项目中的搜索功能,使人们使用URL或使用表单使用GET方法,然后生成的URL。
There's a search feature in my project that enable people to use the GET method using URL or using forms and then generate the URL.
示例:
我要搜索:
产地城市:奥胡斯,丹麦城市和目的地城市:巴拉圭亚松森
origin city: "Århus, Denmark" and destination city: "Asunción, Paraguay"
他们都有一个特殊字符: 和 0 ,这样的URL将这样产生的,当有人点击搜索按钮。
they both have a special character: "Å" and "ó", so the URL will be generated like this when someone click the search button.
?&origin=%C5rhus%2C%20Denmark&destination=Asunci%F3n%2C%20Paraguay
问题:
当我寻找那个城市,它不是像转义我想(我尝试使用如CGI,URI,甚至一些宝石)。
When i search that city, it's not unescaped like i want ( i tried using like CGI, URI, even some gems).
当我在控制台上看到,ActiveRecord的接受查询是这样的:
When i see at the console, ActiveRecord received the query like this:
Parameters: {"destination"=>"Asunci�n, Paraguay", "origin"=>"�rhus, Denmark", "sort"=>"newest"}
City Load (0.1ms) SELECT `cities`.* FROM `cities` WHERE (`cities`.`name` = '�rhus') ORDER BY cities.name ASC
City Load (6.8ms) SELECT `cities`.* FROM `cities` WHERE (`cities`.`name` = 'Asunci�n, Paraguay') ORDER BY cities.name ASC
结论:在城市无法找到的:(
不过,我发现了一个有趣的事情:
But, i found an interesting thing:
-
当我作出asociated使用此功能的文件中存在错误,输出将是这样的:
When i made an error on the file asociated with this function, the output will be like this :
要求
Parameters:
{"destination"=>"Asunción,
Paraguay",
"origin"=>"Århus,
Denmark",
"sort"=>"newest"}
这是一个有效的!
问:
你们是否有一个想法如何解决此问题?在此先感谢:)
Do you guys have an idea how to solve this? Thanks in advance :)
推荐答案
您说得对,它看起来像你有一个编码问题的地方。该0xC5人物是A,在 ISO-8859-1(AKA拉丁-1),在UTF -8这将是%C3%85
在URL中。
You're right, it looks like you have an encoding problem somewhere. The 0xC5 character is "Å" in ISO-8859-1 (AKA Latin-1), in UTF-8 it would be %C3%85
in the URL.
我怀疑你是使用JavaScript在客户端和JavaScript是使用的旧逃跑
功能打造的URL,逃跑
与非ASCII字符的一些问题。如果是这样的话,那么你应该升级你的JavaScript使用<一个href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/en$c$cURIComponent"><$c$c>en$c$cURIComponent$c$c>代替。看一看这个小演示,你就会明白我说的:
I suspect that you're using JavaScript on the client side and that your JavaScript is using the old escape
function to build the URL, escape
has some issues with non-ASCII characters. If this is the case, then you should upgrade your JavaScript to use encodeURIComponent
instead. Have a look at this little demo and you'll see what I'm talking about:
http://jsfiddle.net/ambiguous/U5A3k/
如果您不能更改的客户端脚本,那么你可以做到这一点用在Ruby中艰难地<一href="http://ruby-doc.org/core-1.9.3/String.html#method-i-force_encoding"><$c$c>force_encoding$c$c>和 编码
:
If you can't change the client-side script then you can do it the hard way in Ruby using force_encoding
and encoding
:
>> s = CGI.unescape('%C5rhus%2C%20Denmark')
=> "\xC5rhus, Denmark"
>> s.encoding
=> #<Encoding:UTF-8>
>> s.force_encoding('iso-8859-1')
=> "\xC5rhus, Denmark"
>> s.encoding
=> #<Encoding:ISO-8859-1>
>> s.encode!('utf-8')
=> "Århus, Denmark"
>> s.encoding
=> #<Encoding:UTF-8>
您应该得到类似\ xC5rhus,丹麦
从 PARAMS
,你可以unmangle与
You should get something like "\xC5rhus, Denmark"
from params
and you could unmangle that with:
s = params[:whatever].force_encoding('iso-8859-1').encode('utf-8')
这个服务器端处理将是最后的手段不过,如果您的客户端code被发回正确连接codeD的数据,那么你会留下一堆猜测的服务器上找出编码实际使用获得为URL。
Dealing with this on the server side would be a last resort though, if your client-side code is sending back incorrectly encoded data then you'll be left with a pile of guesswork on the server to figure out what encoding was actually used to get it into the URL.
这篇关于从Rails的3.0.3 URL反向转义的特殊字符正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!