我正在尝试转义特殊字符,例如(空格:/&)以包含在链接中。我尝试了以下方法,但它只会转义 '#' 字符。
<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" 输出是正确的:
谢谢你的帮助