Add a class to each select options with SimpleForm

后端 未结 2 1452
甜味超标
甜味超标 2021-01-13 18:50

I want to build a select input from an array (and not a collection of models), with SimpleForm, and have different classes for each options.

<
相关标签:
2条回答
  • 2021-01-13 19:33

    You also can pass array of arrays as an argument

    = f.input :status, collection: [['option text', 'option_value_1', {"class" => "class_name_1"}], ['option text 2', 'option_value_2', {"class" => "class_name_2"}]]
    
    0 讨论(0)
  • 2021-01-13 19:35

    This appears to be a limitation when using collections, see the author of SimpleForm's explanation here. He recommends a workaround of the form:

    f.input :method, :as => :select do
      f.select :method, [['option text', 'option_value_1', {"class" => "class_name_1"}], ['option text 2', 'option_value_2', {"class" => "class_name_2"}]]
    end
    
    0 讨论(0)
提交回复
热议问题