I am trying to convert hashtags into links within a micropost. I have implemented a gem called simple_hashtag which seems to work for the linking aspect of this. However, the gem works by extracting hashtags from the post and displaying them in their own loop. So, for example if my Micropost was:
Today I went #snowboarding, it was #awesome
The code to extract the tags is done as a loop, which means it has to be done outside of the content.
<p><%= micropost.content %></p>
<ul>
<% micropost.hashtags.each do |hashtag| %>
<li><%= link_to hashtag.name, hashtag_path(hashtag.name) %></li>
<% end %>
</ul>
This leaves the finished view looking like:
Today I went #snowboarding, it was #awesome
- snowboarding
- awesome
Is it possible to have the code scan the content and replace the hashtags with the links in place? I am wondering if there is a way to maybe collect the hashtags, convert them into links and then replace them in the content?
I know I can scan the content and find the hashes using the following:
<% content = micropost.content %>
<% hashes = content.scan(/#\w+/) %>
<% hashes.each do |hash| %>
<%= hash %>
<% end %>
I would then like to use something like hash.gsub!
or even Javascript, to replace the hashtags in the content with the links rather than having them listed below.
I am trying to convert hashtags into links within a micropost. I have implemented a gem called simple_hashtag which seems to work for the linking aspect of this. However, the gem works by extracting hashtags from the post and displaying them in their own loop. So, for example if my Micropost was:
Today I went #snowboarding, it was #awesome
The code to extract the tags is done as a loop, which means it has to be done outside of the content.
<p><%= micropost.content %></p>
<ul>
<% micropost.hashtags.each do |hashtag| %>
<li><%= link_to hashtag.name, hashtag_path(hashtag.name) %></li>
<% end %>
</ul>
This leaves the finished view looking like:
Today I went #snowboarding, it was #awesome
- snowboarding
- awesome
Is it possible to have the code scan the content and replace the hashtags with the links in place? I am wondering if there is a way to maybe collect the hashtags, convert them into links and then replace them in the content?
I know I can scan the content and find the hashes using the following:
<% content = micropost.content %>
<% hashes = content.scan(/#\w+/) %>
<% hashes.each do |hash| %>
<%= hash %>
<% end %>
I would then like to use something like hash.gsub!
or even Javascript, to replace the hashtags in the content with the links rather than having them listed below.
2 Answers
Reset to default 9I would do it using regex, instead of splitting and joining the text
def render_with_hashtags(text)
text.gsub(/(?:#(\w+))/) {hashtag_link($1)}
end
def hashtag_link(hash)
link_to "##{hash}", hashtag_path(hash)
end
You can add html_safe
inside the method if you want.
You could use a helper for this with something like:
<%= render_with_hashtags(micropost.content) %>
in your view, and code like this:
class MicropostHelper
def render_with_hashtags(content)
content_words = content.split(" ")
content_with_links = content_words.map do |word|
if word.contains?("#")
link_to hashtag.name, hashtag_path(hashtag.name)
else
word
end
end
content_with_links.join(" ")
end
end
so, we iterate on each word in the content, replacing the hashtags by links at leaving the rest "as is".