Smilies in Rails using BBCodeizer
I’ve already described how to enable support for Smilies in Rails using RedCloth. If you want to use BBCodeizer instead of RedCloth (Textile), here’s how to do it. We need to extend the BBCodeizer class. We could do this by creating a file in our /lib directory (e.g. my_string.rb), which has to be included in the environment.rb (require “my_string”). Here’s an example for three simple smilies:
# my_string.rb
module BBCodeizer
class << self
Tags[:smiley1] = [/\:\-?\)/, '<img title=":)" src="/images/emoticons/smile.png" alt="smile"/>']
Tags[:smiley2] = [/\;\-?\)/, '<img title=";)" src="/images/emoticons/wink.png" alt="wink" />']
Tags[:smiley3] = [/\:\-?\(/, '<img title=":(" src="/images/emoticons/sad.png" alt="sad" />']
TagList += [:smiley1, :smiley2, :smiley3]
end
end
Appendix: As you can see, i’ve modified the constants Tags and TagList which is at least not good style and throws a warning. To use proper code, you could change the bbcodeizer plugin constants to variables (using lower-case characters, e.g. tags and tag_list). Other possibility is to adjust the constants directly in the plugin.
The smiley shortcuts are now replaced by the according images in any text you pass bbcodeize (e.g.
bbcodeize 'Hi'
returns
'Hi <img title=":)" src="/images/emoticons/smile.png" alt="smile" />'
Dont forget to put some smilies in /images/emoticons Keywords: smiley, smileys, smilie, smilies, rails, bbcode, bbcodeizer, ruby, emoticon, emoticons
Filed under: Development, Emo, English, German, Inspiration, Music, Rails 1.2, Rails 2.0, Rails 2.1, Ruby, Ruby On Rails on August 30th, 2008

[...] There’s also a guide to use Smilies in Rails using BBCodeizer. [...]