开发者

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</opti开发者_运维百科on>

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜