I recently had an issue where I wanted to create a select box populated from an array of continuous years. I wanted to use thecollection_select
method so it would have a minimal “code footprint” and play nice with the form that I had built. The application that I’m working on is in Rails2 and I didn’t want to divert heavily or make to many changes, and after some fiddling I came up with a nice solution: In the environment.rb
file I created an Array constant to contain the year range that I wanted to use:
YEARS_ARRAY = Array.new(89) {|i| 1920+i}
This code is a shortcut to create an array of values between 1920 and 2008 (88 years). It creates a new array with a size of 88, and then uses the i
value to populate each year incrementally. I was pleased to find a method like this!
The form declaration contained the object that I wanted to populate with the form values using the Rails2 syntax:
<% form_for(@movie_poster) do |f| %>
<tr>
<th>Title:</th>
<td><%= f.text_field :title %></td>
</tr>
…..
<% end %>
</table>
*note the newer Rails2 syntax for the form…
I will populate my select box with an Array of Objects. I need to create my Object first to populate my Labels and Values, and I used an “old trick” from Struts by creating a Ruby LabelValue
object that mimics Struts LabelValueBean
utility POJO. The object has a label and value accessor
, which works extremely well for any set of arrays since any kind of mapping scheme would consume duplicate keys and often select boxes, radio buttons, etc may need some duplication. Here is my label_value.rb
class, located in my model directory:
# forms without clearly delinieated# objects, such as Arrays.
class LabelValue
# name the accessors. Label and Value
attr_accessor :label, :value
end
Now that I have a model to work with and an Array Constant to use, I created the “helper” to use with the form tag in the application_helper.rb
file, so I might use it throughout the application’s forms. There are two methods, one public, and one private. Note the “Select year” prompt:
# ‘f’ represents the passed in form value
def year_select(f)
f.collection_select(:year,year_lookup,:label,:value,{:prompt=>”Select year”})
end
# ———— PRIVATE METHODS —————–
private
def year_lookup
#create an emptycollection to hold the LabelValue Objects
years = Array.new()#populate the ArrayYEARS_ARRAY.each do |yr| y = LabelValue.new
y.label = yr
y.value = yr
years.push(y)
end
years
end
Finally, we need to call the _helper method from the form:
<th>Grade:</th>
<td><%= grade_select(f) %></td>
</tr>
The resulting code renders a select box:
<option value=””>Select year</option>
<option value=”1920″>1920</option>
<option value=”1921″>1921</option>
<option value=”1922″>1922</option>
<option value=”1923″>1923</option>
<option value=”1924″>1924</option>
# continues…
<option value=”2005″>2005</option>
<option value=”2006″>2006</option>
<option value=”2007″>2007</option>
<option value=”2007″>2007</option>
</select>
The nice thing is reusability of the LabelValue
class, the YEAR
Constant and the collection_select
itelf. Everything can be accessed as it is needed, whether you want to use the Constant Array for something else, the LabelValue
object for a different array or tuple, or the actual rendered select box in different views.
UPDATE, 1/15:
See the comments below for a nice alternative and discussion!