新手问题 collection_select 如何给下面的 option 自定义属性

tuliang · May 14, 2013 · Last by Martin91 replied at May 14, 2013 · 3952 hits

只能给 select 加属性,后来查文档发现在 model 写方法可以控制 option 的 text 和 value,一直没找到方法能给 option 加自定义属性。例如我要生成这样的 HTML

<select name="post[author_id]">
  <option value="1" age="20">D. Heinemeier Hansson</option>
  <option value="2" age="40">D. Thomas</option>
  <option value="3" age="30">M. Clark</option>
</select>

该怎么做呢?

options_for_select方法,haml 代码举例:

%select{:name => "post[authod_id]"}
  = options_for_select([["D. Heinemeier Hansson", "1", {:age => "20"}], ["D. Thomas", "2", {:age => "40"}], ["M. Clark", "3", {:age => "30"}]])

不过如果一定是要用 collection_select 的话,就不知道了

You need to Sign in before reply, if you don't have an account, please Sign up first.