您正在使用的bb-ruby gem 允许使用作为参数传递给bbcode_to_html 方法的自定义BBCode 翻译。但是,如果您真的希望 ALL 链接包含rel="nofollow",我认为您最好的选择是猴子修补它们本身的宝石。基于BBRuby source,你想这样做:
module BBRuby
@@tags = @@tags.merge({
'Link' => [
/\[url=(.*?)\](.*?)\[\/url\]/mi,
'<a href="\1" rel="nofollow">\2</a>',
'Hyperlink to somewhere else',
'Maybe try looking on [url=http://google.com]Google[/url]?',
:link],
'Link (Implied)' => [
/\[url\](.*?)\[\/url\]/mi,
'<a href="\1" rel="nofollow">\1</a>',
'Hyperlink (implied)',
"Maybe try looking on [url]http://google.com[/url]",
:link],
'Link (Automatic)' => [
/(\A|\s)((https?:\/\/|www\.)[^\s<]+)/,
' <a href="\2" rel="nofollow">\2</a>',
'Hyperlink (automatic)',
'Maybe try looking on http://www.google.com',
:link]
})
end
这将重写 BBRuby 翻译器以始终包含 nofollow 属性。我会将它放在config/initializers 中,并带有描述性文件名,例如bbruby_nofollow_monkeypatch.rb
至于html_safe,我会保持原样。据我了解,这是一种首选方式,并且我认为它可以使您的意图清晰。上面的猴子补丁使您视图中的行更具可读性:
<%= @post.content.bbcode_to_html.html_safe %>