Replacing the 'Auto_Link' Method in Ruby on Rails 3.1

Replacing the 'auto_link' method in Ruby on Rails 3.1

Rinku is a drop-in replacement for Rails 3.1 auto_link.

Auto-linking functionality has been removed from Rails 3.1, and is instead offered as a standalone gem, rails_autolink. You can choose to use Rinku instead.

require 'rails_rinku'

The rails_rinku package monkeypatches Rails with an auto_link method that mimics 100% the original one, parameter per parameter. It's just faster.

Why was auto_link deprecated in Rails?

From the commit where auto_link was removed, Aaron Patterson (tenderlove) explains in the comments:

Unfortunately this method is extremely difficult to secure correctly. Ensuring this method is bullet-proof takes a faster release cycle than we have for rails. That's why we moved it to a gem. Please use the gem if you need the functionality! :-)

https://github.com/rails/rails/commit/81cfbf4146d3c5a58054b64112b8ce196f2fc061

Each security fix only requires updating one gem, instead of the 6 for rails.

auto_link shows plain text HTML in view - Rails 3.0

By default auto_link returns sanitized html_safe strings. This behaviour can be overriden setting the :sanitize option to false

  <%= auto_link("Go to http://www.rubyonrails.org", sanitize: false) %>

Convert plain text URLs to HTML hyperlinks in Ruby on Rails?

Rails 3

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

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

Rails 4

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>"

How do I stop auto_link from escaping angle brackets?

In Rails 3, erb will default to not allow any ruby output to contain html. To get around this you can use "some string".html_safe

<%= auto_link(post.content).html_safe %>

But of course any html or javascript will then be allowed. So...

<%= sanitize(auto_link(post.content).html_safe) %>

rails 3.0.9 auto_link linking text infront of http://

I'm not sure why this particular behavior is exhibited, but I would guess that it has to do with auto_link trying to be protocol agnostic. Aaron Patterson has indicated that this method is inherently difficult to secure and requires a faster release cycle than Rails has, so they have removed it from Rails 3.1 and put it in a separate gem that adds the method back into ActionView::Helpers::TextHelper. You can probably use this gem for Rails 3.0.9 and it might behave differently in this situation.

https://github.com/tenderlove/rails_autolink

Edit:

A quick and dirty hack to fix this for you would be to do the following in a helper:

def your_auto_link(content)
auto_link content.gsub(/([^ ])http:/, '\1 http:')
end

Ruby - rails - how to create automatic hyperlinks for urls in the text/string rendered on the view?

Use auto_link like this:

<%= auto_link(text) %>

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

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

As mentioned by pjumble in the comments, auto_link is no longer a part of Rails core as of Rails 3.1, this gem brings it back: https://github.com/tenderlove/rails_autolink Thanks pjumble!



Related Topics



Leave a reply



Submit