简体   繁体   中英

rails: add a attribute to a option tag through collection_select

I have a collection_select:

f.collection_select(:selected_id, @subcategories, :id, :cat_transl)

which turns into the following tags:

<option value="4">Deutsch</option>
<option value="5">Chinesisch</option>
<option value="6">Spanisch</option>
<option value="10">Mathematik</option>

What I want is to add a attribute to every option

<option value="4" parent="3">Deutsch</option>
<option value="5" parent="3">Chinesisch</option>
<option value="6" parent="3">Spanisch</option>
<option value="10" parent="9">Mathematik</option>

How is this possible?

Thanks Markus

If your really need this attribute despite the fact it's invalid HTML, use a "content_tag" helper method. You can build any tag with it manually.

<%= content_tag(:tag_name, 'text value',  { :value => 'form_value', :anyattr => 'my_val' }) %>

This is not possible using the built-in Rails helpers, probably because it's not valid HTML. You can see which attributes the option element supports here:

http://www.w3schools.com/TAGS/tag_option.asp

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM