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

Rails - HTML Select Tag

2006-04-05   [ 2 comments ]

So here I am, after a recent recess from all things programming, ready to learn more about Ruby, Rails and the Agile way of thinking. And of course as I learn so shall I post many of my experiences, whether complex or quite trivial.

Thus today it shall be nothing more than a trivial exploration into the HTML select tag.

Let us start with the basics, why? good question... because I want to. To create a form using ruby embedded in your .rhtml file simply include the following:

<%= start_form_tag :action => "some_method" %>
... (here is where form elements go) ...
<%= submit_tag 'Submit' %>
<%= end_form_tag =>

To include a select box you only need to add 'select_tag' with any necessary options. The select tag is basically written as so:

<%= select_tag 'tag_name', *option_tags_here* =>

I included a placeholder '*option_tags_here*' to signify that you really have 2 choices in this spot (maybe more that I do not know about). You can either have your options as an array / hash such as:

["Option1","Option2","Option3"]

Or you can include the tag options_for_select which allows you to easily choose a default... to use it simply type:

options_for_select(["Option1","Option2","Option3"], "Option2")

In the above case, Option2 is the default selection.

Well that is all for todays post. Outside of the past 2 weeks, I have been stretching my time between Ruby on Rails, working on several PHP projects, and learning RPG (yes, Report Program Generator - the IBM iSeries native language). I look forward to getting into more advanced topics soon. Adieu.

:author => "Charles Abbott"
Converting to Ruby on Rails
 


Jim said:

Nice. I like the options_for_select tag - didn't know it was around.

I'll have to use that one!

Later

2006-04-14 19:44:55 UTC

Charles said:

Yeah, its pretty neat... I included it in the tag, which can make it kinda big... i.e. <%= select_tag 'thought[frontpage]', options_for_select(["Y","N"], "N") , :options => nil %>

2006-04-14 23:23:44 UTC

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"