Rails form select integer drop-down helper method
I’ve often come across situations while developing Rails apps where I just want a simple integer drop-down box. The default Rails helpers for selects and its options aren’t really geared for something simple like that. I don’t want to have to create a collection of integers and pass them into blocks or any other ridiculous workaround in my views. I want them clean and simple. I created a helper function which allows you to easily create integer drop downs. Just toss this in your application_helper.rb.
[source:ruby]
def select_with_integer_options (object, column, start, stop, default = nil)
output = “
”
end
[/source]
And in your view simply call:
[source:ruby]
< %= select_with_integer_options (:someobject, :someattribute, 1, 20) %>
[/source]
And you have yourself an integer dropdown from 1-20. I tried to make the options and select formatting and id/name conventions the same as the rest of the Rails select/option helper methods to keep things consistent.