【发布时间】:2014-06-25 22:08:26
【问题描述】:
我有一段文本被输入到 textarea 中,如果任何文本与 URI.regexp 匹配,我需要使用 textarea 中 a 标记上的 target: '_blank' 激活该链接。
这是我当前的代码。我还用.match 尝试了这个,这将是正确的
def comment_body(text)
text = auto_link(text)
text.gsub!(URI.regexp) do |match|
link_to(match, match, target: '_blank')
end
end
这个输出:
https://facebook.com">https://facebook.com在我看来
和
<a href="<a href=" https:="" facebook.com"="" target="_blank">https://facebook.com</a> 在被检查的 HTML 中。
在gsub docs 中,它说元字符将按字面意思解释,我认为这让我很困惑。
关于如何正确构建此 URL 的任何提示?
谢谢!
【问题讨论】:
标签: ruby-on-rails ruby link-to gsub