Rails has a few built in helper methods for dealing with links (url_for, link_to, auto_link), but none do exactly what I need:
I want the user to be able to specify a URL, and for me to be able to alter the text it appears as. auto_link almost does what I want, except you can’t change the link text, and it doesn’t recognize links that are typed in like: stackoverflow.com. You have to enter http://www.stackoverflow.com
I want the user to be able to enter something like “stackoverflow.com”, and for me to be able to generate html like this:
<a href="http://www.stackoverflow.com">Username</a>
Is there a plugin out there that adds additional link helper methods?
You can pass a block to auto_link, and the result of that will be the link text. For example:
generates this HTML:
And if you wanted the link text to be different depending on what the URL was, you could do something like:
which generates:
This plus the tips in the comments about adjusting the URL regex seem like they would do what you want.