php url 编码等效于 ruby on rails
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2353742/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
url encode equivalent in ruby on rails
提问by Yuval Karmi
Is there an equivalent to PHP's urlencode in Ruby on Rails 2.3.5? (It encodes a string to be used in a query part of a URL)
I googled it but all the answers seem to date back to before 2006 and seems dates.
This is what I found. It seems a bit abnormal to call CGI::escapein a view.
Ruby on Rails 2.3.5 中是否有与 PHP 的 urlencode 等效的代码?(它编码了一个字符串,用于 URL 的查询部分)我用谷歌搜索了它,但所有的答案似乎都可以追溯到 2006 年之前,而且似乎是日期。
这是我发现的。CGI::escape在视图中调用似乎有点不正常。
Is there an equivalent helper function?
是否有等效的辅助函数?
Thanks!
谢谢!
回答by Mike Trpcic
I believe the uhelper method is what you're looking for:
我相信uhelper 方法是您正在寻找的:
<%=u "URL ENCODE <p>ME</p>" %>
I can't seem to find the documentation for that method, but if I find it in the near future I'll be sure to put a link in here.
我似乎找不到该方法的文档,但如果我在不久的将来找到它,我一定会在这里放一个链接。
Edit: You can find the documentation for this method here.
编辑:您可以在此处找到此方法的文档。
回答by Sam Soffes
If you want to do it without ERB, you can use the following:
如果你想在没有 ERB 的情况下做到这一点,你可以使用以下方法:
Rack::Utils.escape('http://example.com')
#=> "http%3A%2F%2Fexample.com"
回答by yorch
This worked better for me than the Rack::Utils.escape:
这对我来说比以下更好Rack::Utils.escape:
URI::escape('http://example.com/?param=Hello World')
Because it replaced the spaces with %20instead of +
因为它取代了使用空间%20,而不是+
回答by webdevguy
ERB::Util.html_escape, which is aliased to hand ERB::Util.url_encode, which is aliased to u.
ERB::Util.html_escape, 别名为hand ERB::Util.url_encode, 别名为u.
http://ruby-doc.org/stdlib-1.9.3/libdoc/erb/rdoc/ERB/Util.html
http://ruby-doc.org/stdlib-1.9.3/libdoc/erb/rdoc/ERB/Util.html
The method names seem to have changed since Sam Soffes answer, but the aliases haven't.
自 Sam Soffes 回答以来,方法名称似乎已更改,但别名没有更改。

