如何在Rails中URL转义字符串?
问题描述:
如果我在Rails中使用RHTML视图,则很容易进行URL转义:
If I'm in an RHTML view in Rails, it is easy to URL-escape something:
<a href="/redirect?href=<%=u target %>">Foo</a>
我如何在字符串中这样做?我想做这样的事情:
How do I do this in a string? I'd like to do something like this:
<% redirect_href = "/redirect?#{url_escape target}&foo=bar&baz=some_other_stuff" -%>
<a href="<%= redirect_href =>">Foo</a>
这必须是微不足道的吗?
This must be trivial, right?
答
CGI.escape 将会执行:
<% redirect_href = "/redirect?#{CGI.escape target}&foo=bar&baz=some_other_stuff" -%>
<a href="<%= redirect_href =>">Foo</a>