从Rails 3.0.3中的URL正确解码特殊字符 [英] Unescape special characters correctly from the URL in Rails 3.0.3
问题描述
有一个搜索功能我的项目使人们能够使用GET方法使用URL或使用表单,然后生成URL。
示例:
我想搜索:
$ b原始城市:Århus,丹麦和目的地城市:亚美尼亚,巴拉圭他们都有一个特殊字符:Å和ó,所以当有人点击搜索按钮时,URL将被生成。 / p>
?& origin =%C5rhus%2C%20Denmark& destination = Asunci%F3n%2C%20Paraguay
问题:
当我搜索那个城市,这不像我想要的那样(我尝试使用像CGI,URI甚至一些宝石)。
当我在控制台看到时,ActiveRecord收到这样的查询:
参数:{destination=>Asunci n,Paraguay,origin=> rhus,Denmark,sort=>newest}
城市负载0.1ms)SELECT`cities` * FROM`cities` WHERE(`cities`.`name` =' rhus')ORDER BY cities.name ASC
城市负载(6.8ms)SELECT`cities` * FROM`cities` WHERE(`cities`.`name` ='Asunci n,Paraguay')ORDER BY cities.name ASC
结论:找不到城市:(
但是,我发现一个有趣的事情: p>
-
当我对与此功能相关的文件发出错误时,输出将如下所示:
请求
参数:
{destination=>Asunción,
巴拉圭,
origin=>Århus,
丹麦,
sort=>newest}
这是一个有效的! / p>
问题:
你们有一个想法如何解决这个问题吗?感谢提前:)
你说得对,看起来你有一个编码问题。 ISO-8859-1(AKA Latin-1)中的0xC5字符为Å a>,在UTF-8中,URL将是%C3%85
。
我怀疑你在客户端使用JavaScript,并且您的JavaScript正在使用旧的 escape
功能来构建URL, escape
具有非ASCII字符的一些问题。如果是这种情况,那么您应该升级JavaScript以使用 encodeURIComponent
。看看这个小演示,你会看到我在说什么:
http://jsfiddle.net/ambiguous/U5A3k/
如果您无法更改客户端脚本,那么您可以使用 force_encoding
和 encoding
:
>> s = CGI.unescape('%C5rhus%2C%20Denmark')
=> \xC5rhus,Denmark
>> s.encoding
=> #<编码:UTF-8是氢。
>> s.force_encoding('iso-8859-1')
=> \xC5rhus,Denmark
>> s.encoding
=> #<编码:ISO-8859-1>
>> s.encode!('utf-8')
=> Århus,Denmark
>> s.encoding
=> #<编码:UTF-8是氢。
你应该得到一些类似\xC5rhus,Denmark
从 params
,您可以通过以下方式解开:
s = params [:whatever] .force_encoding('iso-8859-1')。encode('utf-8')
在服务器端处理这个将是最后的手段,但如果您的客户端代码发回错误的编码数据,那么您将在服务器上留下一堆猜测,以确定什么编码实际上用于将其转化为URL。
I'm using Rails 3.0.3 with REE ( Ruby 1.8.7 ) and gem 'mysql2', '0.2.6'
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.
Example:
I want to search:
origin city: "Århus, Denmark" and destination city: "Asunción, Paraguay"
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
Problem:
When i search that city, it's not unescaped like i want ( i tried using like CGI, URI, even some gems).
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
Conclusion: the cities can't be found :(
But, i found an interesting thing:
When i made an error on the file asociated with this function, the output will be like this :
Request
Parameters: {"destination"=>"Asunción, Paraguay", "origin"=>"Århus, Denmark", "sort"=>"newest"}
it's a valid one!
Question:
Do you guys have an idea how to solve this? Thanks in advance :)
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.
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:
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>
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')
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屋!