In this case we want the selected value to always be ‘Please select’ so we need to specify that in the ‘options’ hash. If ‘selected’ wasn’t specified in the hash then the code would try to make the selected value @mainFoo.foo_id which in this case has no value anyway.

The other thing which I thought was quite neat is the way that you need to provided the ‘value_method’ and ‘text_method’ as parameters so that the dropdown list can be constructed with the appropriate labels and values.

In this case we have the display values as the keys in the hash and the values as the values in the hash so we can retrieve those entries from the collection by using the ‘:last’ and ‘:first’ methods.

I was initially a bit confused about how we were able to call the ‘collection_select’ method on ‘mainFoo’ but a quick browse of the ActionPack code showed that ‘mainFoo’ actually represents a wrapper around that object rather than the object itself.