0

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!

Dudo
  • 4,002
  • 8
  • 32
  • 57

1 Answers1

1

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.

mind.blank
  • 4,820
  • 3
  • 22
  • 49
  • I think OP is talking about some JavaScript feature, turning url text into link on the fly when typing, just like we input url in SO comment or answer. – Billy Chan May 03 '13 at 02:55
  • negatory. I was just noticing the capability as I typed. This would be info from a model. I have no idea how it works, but that is what I want. do I do this in the view or the controller?? updated original with what I'm trying... – Dudo May 03 '13 at 03:41
  • `undefined method 'auto_link' for #` - I read in some other places about this being a view helper and not playing nicely? – Dudo May 03 '13 at 16:29
  • Yeah that's what I thought might happen... I'm not sure if you can use `URI` in the views either. Try a regex instead, I'm updating my answer. – mind.blank May 03 '13 at 17:03
  • i tried it in a helper as `parse_links(comment)` and i get a bit farther, i think. `undefined method 'empty?' for #` but i don't see any use of `.empty?` anywhere. – Dudo May 03 '13 at 17:09
  • check it. actually put this in a helper method and it worked. was getting the previous error because i was using `parse_links(comment)`. derp. changed that to `parse_links(comment.content)` and it works like a charm! (with the URI in the helper) thanks! – Dudo May 03 '13 at 18:02