K... This seems pretty straight forward, but it's not standardized, it would seem. I've only been looking for a few hours, but hopefully someone here can point me in the right direction.
So, an instance of an Object
has a description. Object has_many comments
. If a user posts a URL in one of those fields, http://www.foodnetwork.com/recipes/ree-drummond/tequila-lime-chicken-recipe/index.html , for instance. As I'm typing this, I see below that somehow, something knew to convert that to a clickable link. I'd like to take that a step further. I'd like to see that same link transformed into just the main url, but still the actual link, a la foodnetwork.
Can rails do something like that on the fly? Is there a gem for something like that? Should I set out to make aforementioned link_bot gem?
After some pointers in the right direction, I went with a helper method, as playing in the model wasn't working. View:
<% if object.comments.any? %>
<% object.comments.each do |comment| %>
<div class='comment block'>
<div class='comment user'>
<%= first_name(comment.user) %>
<span class='comment time'><%= time_ago_in_words(comment.created_at) %> ago</span>
</div>
<div class='comment content'>
<%= parse_links(comment.content) %>
</div>
</div>
<% end %>
<% end %>
In a helper:
def parse_links(comment)
auto_link(comment, html: {target: '_blank'}) do |text|
URI.parse(text).host
end
end
Cheers!