Rails Formtastic: adding “data-” field to option tag

后端 未结 3 908
暖寄归人
暖寄归人 2020-12-06 16:33

I have:

form.input :duration, as: select, collection: {}

I need:

3条回答
  •  自闭症患者
    2020-12-06 17:01

    Actually rails does support adding any kind of html tag to options. Usually you would have:

    options_for_select( [['First', 1], ['Second', 2]] )
    

    which would give you

    
    
    

    If you add a hash into the arrays for each option the hash keys/values will be added as HTML attribute to the option, e.g.

    options_for_select( [['First', 1, {:'data-price' => 20}],
                         ['Second', 2, {:'data-price' => 30}]] )
    

    will produce the required tags:

    
    
    

提交回复
热议问题