简体   繁体   English

Rails:如何避免更改URL中的空格和其他特殊字符

[英]Rails: How to avoid changing spaces and other special characters in URLs

I'm having some trouble with characters like spaces, plus signs, double quotes and accented latin characters in rails when adding them to urls as parameters. 在将它们作为参数添加到url时,我在使用空格,加号,双引号和带重音的拉丁字符等字符时遇到了麻烦。 They are always converted to numbers preceded by %, and this is generating a lot of trouble for us, since portuguese uses a lot of those characters. 它们总是转换为以%开头的数字,这给我们带来了很多麻烦,因为葡萄牙语使用了很多这样的字符。

Everything works just fine when typing the character manually in the URL, but once rails makes it into a link, it'll replace it. 在URL中手动键入字符时,一切工作正常,但是一旦rails将其放入链接中,它将替换掉它。

Is there a way to avoid that? 有办法避免这种情况吗?

Here's an example. 这是一个例子。 Instead of 代替

url?q=transgênico

we get 我们得到

url?q=transg%C3%AAnico

This completely breaks our search and communication with other websites through parameters - which works fine when typing the special characters manually. 这完全破坏了我们通过参数与其他网站的搜索和通信-在手动键入特殊字符时效果很好。

I must admit I did not search about this, since english is not my first language and I have no clue what to search for... All my tries had no results, but I probably was using bad terms. 我必须承认我没有搜索过此内容,因为英语不是我的母语,而且我也不知道要搜索什么。

Using Rails 2.3.8. 使用Rails 2.3.8。

Thank you in advance. 先感谢您。

我相信您必须对这些字符进行编码,因为它们在url中无效: 统一资源定位符(URL)规范stackoverflow

I'd try setting the :escape => false flag in link_to and the like. 我会尝试在link_to等中设置:escape => false标志。 If that doesn't help you'll have to monkey patch actionpack/lib/action_view/helpers/url_helper.rb probably. 如果那没有帮助,则可能必须修补actionpack/lib/action_view/helpers/url_helper.rb

Maudite is right. 莫德是正确的。 You can convert this urls back to a 'nice' form before using it as search terms: 您可以在将其用作搜索词之前将其转换回“ nice”形式:

require 'cgi'
CGI.unescape 'url?q=transg%C3%AAnico'

produces: 产生:

"url?q=transgênico"

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

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