Well, as you can already guess - this will most likely be a short post considering that the subject simply covers a few basic features of the rails api. The first two I will cover are related, while the third is also another extension of the ActionView Class... and it is (in my opinion) really helpful if you plan on leaving mailto links on your site.
So, whats first? Select boxes are so useful in making choices quick and simple. However simple they may be for the user, they are unfortunately often tedious to setup all the options necessary. A big thanks goes to the Rails API for providing one shortcut in providing Country options for your next select box. If you just need the options themselves use this in your view where necessary:
<%= country_options_for_select() %>
If you want to preselect one of the options simply pass it as a string:
<%= country_options_for_select("United States") %>
So thats great... you can easily get a bunch of generated option tags that have more countries than I care to count. You can also pass it a group of countries you would like to appear at the top of the selection list simply by:
<%= country_options_for_select("United States", ["Belize", "Canada", "United States"]) %>
However easy all this seems,
I was unable to get multiple selected options to generate.
I tried passing an array and a hash, but no luck on my end. (I was able to get the select box to allow multiple selections) If someone else has any luck with this feel free to leave a comment with the proper format for multiple selected options.
The second part (which is quite similar to the first) simply prints out the countries as options along with the select box. So, with one fell swoop you can create the select box and the options:
<%= country_select "countries", "selection", [ "Belize", "Canada", "United States"] %>
In this example I create a select box with id = countries_selection and name = countries[selection]. While the simple array provides a list of "priority countries" that are first in the list.
Well, now that we have spent some time on the country select options, lets move on to the mail_to function.
The mail_to simply creates those nifty little links that help people launch their favorite mail client with prefilled options to send an email. Rails makes creating these links a breeze with the mail_to function. Allow me to give just a basic example - and break down the options as they are:
<%= mail_to "charles@nospam.com", "Charles Abbott", :encode => "hex", :cc => "charles@nospam.com", :bcc => "charles@nospam.com", :subject => "This is RoR making...", :body => "..my life easier!" %>
This is so simple I love it!
First provide mail_to with the main To: email address, then the text for the link. The next option is very helpful in stopping bots from harvesting your email address from your website - :encode => "hex" will encode your address in the source with the equivilant hex characters.
The remaining options are pretty straightforward, :cc => creates the carbon copy, :bcc => creates the blind carbon copy, :subject => prefills the subject line of the message, and :body => sets up a preformatted email body.
All of that with one simple, easy to read line! Very nice indeed - however basic it may be...
Well, that is it for this post - after that brief vacation to Atlanta, I had to write something to get back on track (rails!). Happy coding =)
