0

我正在尝试转义特殊字符,例如(空格:/&)以包含在链接中。我尝试了以下方法,但它只会转义 '#' 字符。

<a href="https://twitter.com/share?text=<%=location.title.html_safe%><%=h root_url(:anchor => "_lat=#{location.latitude}&long=#{location.longitude}&zoom=17").html_safe%>">Tweet</a>

我也试过<%=CGI.escapeHTML location.title%><%=CGI.escapeHTML root_url(...)%>它不会转义空格或 root_url 中的 http://

谢谢。

编辑 谢谢Sirupsen,这是我最终做的,现在输出是正确的,除了空格被转义为+,它不确定它是否是最好的......

<!-- added var to make it more readable-->
<% tweetLink = root_url(:anchor => "_lat=#{location.latitude}&long=#{location.longitude}&zoom=17")%>
<a href="https://twitter.com/share?text=<%= URI.encode_www_form_component "#{location.title} #{tweetLink}"%>">Tweet</a>

根据 location.title ="http:// test in title" 输出是正确的:

https://twitter.com/share?text=http%3A%2F%2F+test+in+title+http%3A%2F%2Flocalhost%3A3000%2F%23_lat%3D43.82864423876971%26long%3D-79.36823174357414% 26zoom%3D17和推特接受它。

谢谢你的帮助

4

0 回答 0