comments url helper - using rails - LinkBot?

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'>&nbsp;&nbsp;
        &nbsp;&nbsp;<%= 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!

Answers


You should try the auto_link gem.

They give an example of playing around with the link text:

post_body = "Welcome to my new blog at http://www.myblog.com/.  Please e-mail me at me@email.com."
auto_link(post_body, :html => { :target => '_blank' }) do |text|
  truncate(text, 15)
end
# => "Welcome to my new blog at <a href=\"http://www.myblog.com/\" target=\"_blank\">http://www.m...</a>.
Update

Try this:

<%= auto_link(comment.content, html: {target: '_blank'}) do |text| %>
   # if URI.parse(text).host doesn't work try a regex:
   <%= text.match(/http:\/\/([^\/]*).*/)[1] %> # is there a better way to do this regex?
   # also try just <% instead of <%= if you get weird outputs.
<% end %>

If you get any errors add them to your question.


Need Your Help

how to stop the timer in android

android timer uiimageview

This is my code following,I am changing some iamges dynamically in my image view.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.