Ok I am creating a gem that is supposed to find tags #
, @
, or $
in user's posts. I am using a case when statement and it seems to work, only sometimes. For example I will have a string like @you
and that works, but #cool
does not work unless I add #cool @you
. It seems the other when
statements only work if the first when
statement is true. The REGEX is just so it knows what to look for and I know those do work.
REGEXS = [Supertag::Tag::USERTAG_REGEX, Supertag::Tag::HASHTAG_REGEX, Supertag::Tag::MONEYTAG_REGEX]
def linkify_tags(taggable_content)
text = taggable_content.to_s
REGEXS.each do
case text
when text = text.gsub(Supertag::Tag::USERTAG_REGEX) {link_to($&, usertag_path($2), class: 'tag')}
when text = text.gsub(Supertag::Tag::HASHTAG_REGEX) {link_to($&, hashtag_path($2), class: 'tag')}
when text = text.gsub(Supertag::Tag::MONEYTAG_REGEX) {link_to($&, moneytag_path($2), class: 'tag')}
end
end
text.html_safe
end
See Question&Answers more detail:os