Caution! - Many of these posts are creepy-old in the Ruby on Rails world (before 1.0!)
The :author => Charles Abbott now blogs here

country_select, country_options_for_select, mail_to

2006-09-05   [ 0 comments ]

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 =)

:author => "Charles Abbott"
Ruby and Rails - Programming Euphoria
 

What?

Who?              Link?



Frameworks Good or Bad?   :date => "2007-10-06"
Where is ForTheCode.com Going?   :date => "2007-09-23"
Refactoring - Vital to Software Development   :date => "2007-09-23"
Mongrel Cluster a quick note - and extra notes   :date => "2007-05-20"
Linux Mongrel and Rails   :date => "2007-05-15"
form_remote_tag revisited   :date => "2007-01-07"
How To: Ubuntu 6.10 Edgy on Rails part 3   :date => "2006-12-30"
How To: Ubuntu 6.10 Edgy on Rails part 2   :date => "2006-12-24"
How To: Ubuntu 6.10 Edgy on Rails   :date => "2006-12-22"
verify ... 5.times do cycle   :date => "2006-09-25"
country_select, country_options_for_select, mail_to   :date => "2006-09-05"
Generate and Send Email in Rails   :date => "2006-08-26"
FDF Model, gsub, and send_data   :date => "2006-08-18"
Active Directory Authentication with acts_as_authenticated   :date => "2006-08-17"
Apache2 proxy with Lighttpd - FastCGI for Rails   :date => "2006-08-08"
reverse! && a simple file Upload Class   :date => "2006-07-29"
send_file - a link to download a file   :date => "2006-07-24"
Environments (production, development, test) and cache_pages   :date => "2006-07-04"
.class .methods .instance_variables   :date => "2006-06-14"
select_tag :multiple => true   :date => "2006-06-01"
FileUtils, action_controller rescues   :date => "2006-05-20"
file_field_tag, File.size, File.path, FileUtils.mv   :date => "2006-05-15"
javascript_include_tag, stylesheet_link_tag   :date => "2006-05-02"
submit_to_remote, form_remote_tag, script.aculo.us   :date => "2006-04-30"
periodically_call_remote, simple_format   :date => "2006-04-26"
observe_field - Ajax!   :date => "2006-04-21"
h method, TextHelper, sanitize(), strip_tags()   :date => "2006-04-15"
Rails API :My API [.count(), link_to, text_area :size]   :date => "2006-04-13"
Rails - HTML Select Tag   :date => "2006-04-05"
Pruning Old Sessions   :date => "2006-03-21"
If Elsif Else, and Searching Too!   :date => "2006-03-17"
SHA1 - A quick update   :date => "2006-03-15"
Initialized! good, Authorized? Great! part 2   :date => "2006-03-11"
Initialized! good, Authorized? Great!   :date => "2006-03-08"
Forms and Routing in RoR   :date => "2006-03-06"
My First RoR Post !   :date => "2006-03-05"