2

I am working on a Rails 2.3.9 application . I need to pass a class to the options. The way i am doing is

<%= select(:resporg, "hresporg_#{row_id}",options_for_select(["Item11",["Item 2", {:class => 'has-versions'}]]), {}, {:multiple => true})%>

Output

<select id="resporg_hresporg_1" name="resporg[hresporg_1][]" multiple="multiple">
<option value="Item11">Item11</option>
<option value="classhas-versions">Item 2</option>
</select>

If you check the above output the class is coming as value. Any idea on what is going wrong?

AnkitG
  • 6,438
  • 7
  • 44
  • 72

2 Answers2

1

According to this,

['display','value',:class => 'option_class']

will produce

<option value="value" class="option_class">display</option>

So in your case, I would try this:

["Item 2", "Item2", :class => 'has-versions']
siekfried
  • 2,964
  • 1
  • 21
  • 36
  • It's for >= v3.0.0.. Rails 3 it work's fine... i am working on rails 2.3.9 . I can't update rails – AnkitG Feb 07 '13 at 14:11
1

I have to manually do it because of the constraint. That this is valid for rails >= 3.0.0

In the template.html.erb

<select style="width:160px !important;" id="resporg_hresporg_1" name="resporg[hresporg_1][]" multiple="multiple">
      <%= opts %>
</select>

In the helper

def opts
    str = ''.html_safe
    ['Item1','Item2'].each { |val| str << content_tag(:option, val, :value => val,:class => val.eql?('Item2') ? "MyClass" : nil) }
    str.html_safe
end
AnkitG
  • 6,438
  • 7
  • 44
  • 72