在 Ruby on Rails 中将纯文本 URL 转换为 HTML 超链接? [英] Convert plain text URLs to HTML hyperlinks in Ruby on Rails?

查看:45
本文介绍了在 Ruby on Rails 中将纯文本 URL 转换为 HTML 超链接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本区域,用户可以在其中输入大量可能包含超链接的文本.如何显示文本并使 URL 自动显示为超链接?是否有执行此操作的 gem、插件或现有方法?我希望能够执行以下操作:
<%=h @my_object.description.with_links %> 在我看来.

I have a text area into which users enter a lot of text that potentially includes hyperlinks. How can I display the text and have the URLs automatically appear as hyperlinks? Is there a gem, plugin, or existing method that does this? I'm looking to be able to do something like:
<%=h @my_object.description.with_links %> in my view.

推荐答案

Rails 3

使用提供的名为 auto_link 的辅助方法,这是 ActionPack 的一部分.

Use the provided helper method called auto_link, which is part of ActionPack.

<%=h auto_link(@my_object.description) %>

Rails 4

自动链接已从 Rails 中删除,并已纳入 rails_autolink 宝石.

Auto-linking has been removed from Rails and has been made into the rails_autolink gem.

require 'rails_autolink'

auto_link("Go to http://www.rubyonrails.org and say hello to david@loudthinking.com")
# => "Go to <a href=\"http://www.rubyonrails.org\">http://www.rubyonrails.org</a> and
#     say hello to <a href=\"mailto:david@loudthinking.com\">david@loudthinking.com</a>"

这篇关于在 Ruby on Rails 中将纯文本 URL 转换为 HTML 超链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆