Ruby - rails - how to create automatic hyperlinks for urls in text / line displayed in view?

How to create automatic hyperlinks for URLs in the text / line displayed in the view? I have a page that displays a user’s activity log, and in this log / text / line there are some random URLs that I automatically want hyperlinks to open in a new browser window. Is it auto_link in ruby ​​rails, how can I use it?

 text = "User xyz accessed url - http://www.something.com on 04/13/2012 00:13:18GMT" <%= "#{Text}" %> 

I want this to display with a hyperlink to the URL. The URL can be anything in the text.

+6
source share
2 answers

Use auto_link as follows:

 <%= auto_link(text) %> 

If you want the generated links to open new browser windows (or tabs), add this option:

 <%= auto_link(text, :html => { :target => '_blank' }) %> 

As mentioned in the comments, auto_link no longer part of the Rails core, but Rails 3.1, this pearl returns it: https://github.com/tenderlove/rails_autolink Thanks pjumble!

+13
source

For faster parsing, try https://github.com/vmg/rinku

 require 'rinku' Rinku.auto_link(text, mode=:all, link_attr=nil, skip_tags=nil) 

It also often requires more filters than just linking, such as embedded Youtube videos. To do this, use: https://github.com/dejan/auto_html

+1
source

Source: https://habr.com/ru/post/913221/


All Articles