Method: Nitro::XhtmlHelper#options

Defined in:
lib/nitro/helper/xhtml.rb

#options(options = {}) ⇒ Object

Render select options. The parameter is a hash of options.

labels

The option labels.

values

The corresponding values.

labels_values

Use when labels == values.

selected

The value of the selected option.

Examples

labels = [‘Male’, ‘Female’] o.select(:name => ‘sex’)

o.options(:labels => labels, :selected => 1)

or

#:labels => labels, :values => [..], :selected => 1 #:options, :labels => labels, :values => [..], :selected => 1



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/nitro/helper/xhtml.rb', line 63

def options(options = {})
  if labels = options[:labels] || options[:labels_values]
    str = ''
    
    values = options[:values] || options[:labels_values] || (0...labels.size).to_a
    
    selected = options[:selected]
    selected = selected.to_s if selected
    
    labels.each_with_index do |label, idx|
      value = values[idx]
      if options[:style]
        style = if options[:style].is_a?(Array) 
          options[:style][idx]
        else
          options[:style]
        end
        style = %{ style="#{style}"}
      end
      if value.to_s == selected
        str << %|<option value="#{value}" selected="selected"#{style}>#{label}</option>|
      else
        str << %|<option value="#{value}"#{style}>#{label}</option>|
      end
    end
    
    return str
  else
    raise ArgumentError.new('No labels provided')
  end
end