百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
R

rails_autolink

> 编程语言
开源

Rails 中的 auto_link 函数

618 stars0 点赞1 次浏览
访问官网GitHub

工具介绍

Rails 中的 auto_link 函数

rails_autolink

  • http://github.com/tenderlove/rails_autolink

Description

This is an extraction of the auto_link method from rails. The auto_link method was removed from Rails in version Rails 3.1. This gem is meant to bridge the gap for people migrating.

Features

By default auto_link returns sanitized html_safe strings. This behaviour can be overridden by setting the :sanitize option to false (thus making it insecure if you don't have the content under control).

Install

Add this line to your application's Gemfile:

ruby
gem 'rails_autolink'

And then execute:

bash
$ bundle install

Synopsis

Basic Usage

ruby
require 'rails_autolink'

auto_link("Go to http://www.rubyonrails.org and say hello to [email protected]")
# => "Go to http://www.rubyonrails.org and
#     say hello to david@loudthinking.com"

Convert Only URLs to Links

ruby
auto_link("Visit http://www.loudthinking.com/ or e-mail [email protected]", :link => :urls)
# => "Visit http://www.loudthinking.com/
#     or e-mail david@loudthinking.com"

Convert Only Email Addresses to Links

ruby
auto_link("Visit http://www.loudthinking.com/ or e-mail [email protected]", :link => :email_addresses)
# => "Visit http://www.loudthinking.com/ or e-mail david@loudthinking.com"

Generate Links Without Sanitizing HTML Tags

ruby
## By default, HTML tags are sanitized to protect from malicious code
auto_link("Go to http://www.rubyonrails.org 
")
# => "Go to http://www.rubyonrails.org "

## Use the :sanitize => false option to prevent sanitization
auto_link("Go to http://www.rubyonrails.org 
", :sanitize => false)
# => "Go to http://www.rubyonrails.org 
"

Customize Links and Shorten Text

ruby
post_body = "Welcome to my new blog at http://www.myblog.com/.  Please e-mail me at [email protected]."
auto_link(post_body, :html => { :target => '_blank' }) do |text|
  truncate(text, :length => 15)
end
# => "Welcome to my new blog at http://www.m....

Requirements

  • rails > 3.1

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Ruby

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言