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.

def select_with_integer_options (object, column, start, stop, default = nil)
output = “


end

And in your view simply call:

< %= select_with_integer_options (:someobject, :someattribute, 1, 20) %>

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.

2 Responses to “Rails form select integer drop-down helper method”

  1. ncr Says:

    Nice! Did you forget to close the option tags (), or is this on purpose?

    Greets Nico

  2. ncr Says:

    Also you don’t have the nice error field highlighting built into the rails select helper…

Leave a Reply