In building the View part (using RHTML Templates) of a RoR (Ruby on Rails) application we frequently want to present a controlled set or list of options for the user to select and way to do this is using the html select tag, which is easy enough to do in RoR, either building it with embedded Ruby (ERb) and/or using the select() helper method, and with only a little more effort you can do so using information stored in a database table via your models, of course, using a find() method to get the values. The “Agile Web Development with Rails” AWDR book has some excellent information on doing this (Chapter 17 pages 357 – 359 ).

The collection_select helper adds in some extra sugar automatation dealing with the collection.

The element present to the users to select are based on the values of only one field. What if you what to be more descriptive and present more information?

The use of the selected=”selected” attribute is necessary when you are editing an existing record – easy, but also for new elements there is no way to present a default element, unless the first element is the default. But what if you what to set Manitoba as the default in a list of provinces (M being in the middle), or present a blank choice or a “Please Select a Widget Type” message?

With this goal in mind I set out to build my more user friendly generated Select.

now for some explaining (assuming some knowledge of ruby and rails): we are updating a stuff table/model with a field called “item_id”, there is also a table/model call item with fields “id”, “name”, and “description”

line 1: start the select tag. the name=”stuff[item_id]” is important to populate the “item_id” field in “stuff”

lines 2-4: checking the value in for the current record of stuff. When a new record is created it is zero, when it has failed validation it is “nil”. In either case this is where the default message (which might just be an emplty string), and value goes. You could also do a different default with validation fails.

line 5: gets all the values from the Item model, sorted by name, and stores it in @items.

line 6: start looping thru @items, one item at a time.

line 7: builds each option tag of the select and sets the value attribute to “item.id”, which is what will be actually stored in “stuff”.

line 8: sets the selected attribute if we are editing an existing “stuff” record.

line 9: close the front part of the option tag.

line 10: build what the user see, lin this case the name and in brackets the description

line 11:then close the option tag for this value of item.

line 12: close the looping for this item , which was started on line 6.

line 13 :close the select tag.

Hopefully this is clear enough to help others. I’m still exploring the documentation and the RoR api doc has a section on ActionView::Helpers::FormOptionsHelper, with some specialized select Helpers.

I did not address the “Manitoba as the default in a list of provinces (M being in the middle)” objection in the above code. To do so you have have to A) know or figure out that “Manitoba” had a value of 6, then B) , my guess at best – or least worst – practice, do a

1

if @stuff.item_id == 0 then @stuff.item_id = 6

to set this default value before the select.

Perhaps I should I will build a re-useable helper “collection_select_with_defaults”? …very very soon.

Found this yesterday via Google. I’ve been playing with Ruby and Rails for a week and a half, and wondered why there wasn’t any way to include a prompting value in a select tag. I’ve put together a tag helper that acts just like Rails’ “collection_select” method, but which takes an extra option called :first_row.

The following code assumes I have a Client model object, and that I’ve declared a method on it called “name_first_last_active” that shows a display-friendly name. (This fits well in a mixin called “Person,” by the way.)

# If we were given text for the first row, add another option tag with it first_row = options[:first_row] unless first_row.nil? first_row_html = content_tag(:option, first_row, :value => ”) options_html = “#{first_row_html}\n#{options_html}” end

how to have multiple selection option … “multiple” = “true” is showing combo box in which i can select more than one values … but not getting how to fetch those selected values in controller … Kindly help …