2008-05-28
Rails的表单helper: select
关键字: rails form select
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元素的写法为:
将生成
:include_blank=>true表示生成一项空选项。
在线的rails API文档在编程时非常有用,查找方便,位置在
http://www.railsapi.org:8100/
引用
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/
- 18:33
- 浏览 (95)
- 评论 (4)
- 分类: ruby & rails
- 进入论坛
- 相关推荐
评论
seemoon
2008-06-21
rails提供了不在form_for下的select标签helper 'select_tag',这个select_tag方法的使用颇有不同:
通过另一个方法options_for_select来生成select的option元素,实现方式比较怪异,不清楚为什么不做得像form_for里的select方法,这样一致性会好很多。
<%=select_tag :sth_id, options_for_select(@groups,selected), {:onchange=>"doSth();"}%>
通过另一个方法options_for_select来生成select的option元素,实现方式比较怪异,不清楚为什么不做得像form_for里的select方法,这样一致性会好很多。
1piece
2008-05-29
seemoon 写道
补充一下,如果选项数据从数据库中获取,通过调用查询返回数组的collect方法可以快捷组装成符合select要求的选项数据,比如以省份表provinces的数据为例
class Province < ActiveRecord::Base
def self.list_options
find(:all, :select=>"id,name", :order=>"name").collect{|p| [p.name,p.id]}
end
end
Province.list_options 返回[['四川',1],['浙江',2],...]
class Province < ActiveRecord::Base
def self.list_options
find(:all, :select=>"id,name", :order=>"name").collect{|p| [p.name,p.id]}
end
end
Province.list_options 返回[['四川',1],['浙江',2],...]
这里的select感觉只是个叠代器一样功能的,和之前的select :name,optipns 不一样吧?要是可是直接生成下拉菜单倒是很方便啊
seemoon
2008-05-29
补充一下,如果选项数据从数据库中获取,通过调用查询返回数组的collect方法可以快捷组装成符合select要求的选项数据,比如以省份表provinces的数据为例
class Province < ActiveRecord::Base
def self.list_options
find(:all, :select=>"id,name", :order=>"name").collect{|p| [p.name,p.id]}
end
end
Province.list_options 返回[['四川',1],['浙江',2],...]
class Province < ActiveRecord::Base
def self.list_options
find(:all, :select=>"id,name", :order=>"name").collect{|p| [p.name,p.id]}
end
end
Province.list_options 返回[['四川',1],['浙江',2],...]
xuchunming
2008-05-28
另外还有 options_from_collection_for_select
和 option_group_form_selection_for_select
也很强大 不过option_group_form_collection_for_selelct 只支持两级 多级就不支持了
和 option_group_form_selection_for_select
也很强大 不过option_group_form_collection_for_selelct 只支持两级 多级就不支持了
发表评论
提醒: 该博客已发表在公共论坛,博客所有留言会成为论坛回贴,留言请注意遵守论坛发贴规则
- 浏览: 4254 次
- 性别:

- 来自: 上海

- 详细资料
搜索本博客
最近加入圈子
最新评论
-
单元测试势在必行的一个例 ...
open2ye 写道以上代码还有错误 save! 是抛错的.save 返回值才是 ...
-- by caryl -
开始攻克英语听力之旅(二 ...
fxbird 写道我怎么觉得这是一种隐性广告。我练听力目前用新概念3,听力其实就 ...
-- by Durian -
开始攻克英语听力之旅(二 ...
回复asd: 很抱歉,在我写的博客上, http://seemoon.javae ...
-- by seemoon -
开始攻克英语听力之旅(二 ...
老大,你的一呢,能不能给个link学习下,我往后翻了几页没有找到。
-- by asd -
开始攻克英语听力之旅(二 ...
我怎么觉得这是一种隐性广告。我练听力目前用新概念3,听力其实就是看词汇量,词汇量 ...
-- by fxbird






评论排行榜