rails的actionviews提供了友好的form builder类来简洁的动态生成表单元素,相比于简单的textfield,select helper有一些特别,传入参数较多,根据rails的api文档,对select的使用说明如下:
引用
select(object, method, choices, options = {}, html_options = {})
object是指select选项所修饰的目标对象,method是目标对象的属性(方法)名, choices是一个数组,包含了选择项的‘name-value’值,options和html_options是选项。以person为例,person有性别gender属性,选项有[['男',0],['女',1]],用select来生成select元素的写法为:
<%=select :person,:gender,[['男',0],['女',1]], {:include_blank=>true,:selected=>0}%>
将生成
<select name="person[gender]">
<option value=""></option>
<option value="0" selected="selected">男</option>
<option value="1">女</option>
</select>
:include_blank=>true表示生成一项空选项。
在线的rails API文档在编程时非常有用,查找方便,位置在
http://www.railsapi.org:8100/