javascript - Turning hashtags to links in place, in rails -


i trying convert hashtags links within micropost. have implemented gem called simple_hashtag seems work linking aspect of this. however, gem works extracting hashtags post , displaying them in own loop. so, example if micropost was:

    today went #snowboarding, #awesome 

the code extract tags done loop, means has done outside of content.

    <p><%= micropost.content %></p>     <ul>         <% micropost.hashtags.each |hashtag| %>             <li><%= link_to hashtag.name, hashtag_path(hashtag.name) %></li>         <% end %>     </ul> 

this leaves finished view looking like:

    today went #snowboarding, #awesome     - snowboarding     - awesome 

is possible have code scan content , replace hashtags links in place? wondering if there way maybe collect hashtags, convert them links , replace them in content?

i know can scan content , find hashes using following:

    <% content = micropost.content %>     <% hashes = content.scan(/#\w+/) %>     <% hashes.each |hash| %>         <%= hash %>     <% end %> 

i use hash.gsub! or javascript, replace hashtags in content links rather having them listed below.

you use helper like:

<%= render_with_hashtags(micropost.content) %> 

in view, , code this:

class microposthelper   def render_with_hashtags(content)     content_words = content.split(" ")     content_with_links = content_words.map |word|        if word.contains?("#")         link_to hashtag.name, hashtag_path(hashtag.name)       else         word       end     end      content_with_links.join(" ")   end end 

so, iterate on each word in content, replacing hashtags links @ leaving rest "as is".


Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -