comments url helper - using rails - LinkBot? -


k... seems pretty straight forward, it's not standardized, seem. i've been looking few hours, here can point me in right direction.

so, instance of object has description. object has_many comments. if user posts url in 1 of fields, http://www.foodnetwork.com/recipes/ree-drummond/tequila-lime-chicken-recipe/index.html , instance. i'm typing this, see below somehow, knew convert clickable link. i'd take step further. i'd see same link transformed main url, still actual link, la foodnetwork.

can rails on fly? there gem that? should set out make aforementioned link_bot gem?

after pointers in right direction, went helper method, playing in model wasn't working. view:

<% if object.comments.any? %>   <% object.comments.each |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 helper:

def parse_links(comment)   auto_link(comment, html: {target: '_blank'}) |text|     uri.parse(text).host   end end 

cheers!

you should try auto_link gem.

they give example of playing around link text:

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

update

try this:

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

if errors add them question.


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -