Class: Array

Inherits:
Object
  • Object
show all
Defined in:
lib/acts_as_dropdown.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#to_options_for_select(text = :name, value = :id, include_blank = false) ⇒ Object Also known as: to_dropdown

Collects the contents of the array and creates a new array that can be easily used with the select form helper method.

Options

  • text - This is the attribute that will be used as the text/label for the option tag (defaults to ‘name’).

  • value - This is the attribute that will be used to fill in the option’s value parameter (defaults to ‘id’).

  • include_blank - Specify true if you’d like to have a blank item added to the beginning of your aray, or

    a string that will be placed in the value attribute of the option group.
    

Example

>> @states = State.find(:all, :order => "id")
>> @states.to_dropdown 
=> [["Alabama", 1], ["Alaska", 2], ["Arizona", 3], ["California", 4], ["Colorado", 5]]


160
161
162
163
164
165
166
167
168
# File 'lib/acts_as_dropdown.rb', line 160

def to_options_for_select(text = :name, value = :id, include_blank = false)
  items = self.collect { |x| [x.send(text.to_sym), x.send(value.to_sym)] }

  if include_blank
    items.insert(0, include_blank.kind_of?(String) ? [include_blank, ""] : ["", ""])
  end

  items
end