I want to check a string and change any @something to link. So I have a helper function which consists of something like this:
def parse(content)
content.gsub(/@[a-zA-z0-9]+\b/, link_to("#{$1}", user_path($1)) )
end
But the result is <a href="/users/102"></a>
The problem is :
- The
<a href="/users/102"></a>is a string, because somehow the<and>is escaped. - Why does
"#{$1}"return nothing? Isn’t it supposed to return whatever is checked upon, in this case@something?
<%= raw parse(content) %>in your view.gsub:So you can’t use
#{$1}because$1isn’t set until after the command has finished. Your best bet is probably to use the block form of gsub – in which case$1is set inside the block. Try: