This is a really simple code example, but what I love about it, is it shows what a pleasure Ruby is to write in! I wanted to add a "Tell a Friend" feature to the front page. First and foremost, it should be functional, but it also shouldn’t be another boring form wrapped in a template. This is where the Redbox plugin really spiced things up! I’ve used the Redbox plugin before, and what it is, is a lightbox plugin for Rails. A usual application of lightboxing is seen when looking through an image gallery, and the main image comes front and center, while the rest of the screen gets a dark grey overlay. Lightboxes can be used for any content, paragraphs, images, and in my case a form to be filled out.
Step 1 was to get a snappy new button designed, and I can’t take the credit for that. In fact, I can’t take the credit for any of the fantastic graphic art on the site. That is where the super talented graphic artist, Brian, comes in. He sent over a spiffy new "Tell a Friend" button to be used as the anchor of the feaure.
Step 2, install the Redbox plugin and get ready to use it! We used the link_to_redbox call for this feature, and embedded the content for the Redbox in a hidden div. To use the image graphic as the link, you can do something like this:
<%= link_to_redbox((image_tag "/taf.jpg"), "taf") %>
Step 3, create the form in the "taf" hidden div. No magic here, just a simple form, and don’t forget to use all the fantastic Rails Form Helpers.
Step 4, make sure the "to" field can parse multiple entries. You want users to be able to type in a list of email addresses, not just one, so some additional code needs to be added to the controller that is handling the form action. This is where the Ruby syntax, especially working with the String Class is so great. All we have to do is split the String passed in from the "to" field. Here we are looking for all the commas, and splitting the String into an Array of email addresses, called "Tos".
Tos = @taf.to.split(',')
From there it is a simple for loop to actually deliver the send to a friend email message to all the addresses in the "Tos" array:
for To in Tos
UserNotifier.deliver_taf(To, @taf.from, @taf.msg)
end
And that’s about it! A couple of notes … Even though the latest version of the Redbox plugin says that it is compatible with IE, I had trouble getting it to work properly. I tried it on IE 7.0, and the grey shadow that is suppose to cover the screen would not cover all the divs I had in the template. For IE, I had to resort to old "if / else" trick, where if it is an IE browser, don’t do the Redbox, just go to a boring form page. All the other users, with the right browsers, will still get to use the fancy Redbox’d form. But if you’ve ready my blog at all, you’ll know my battles and scars with IE run deep.