rails select使用小结

最近经常使用select下拉标签

个人比较喜欢用select 和 select_tag 两个


[b]select_tag(name, option_tags = nil, options = {}) [/b]
Creates a dropdown selection box, or if the :multiple option is set to true, a multiple choice selection box.

Helpers::FormOptions can be used to create common select boxes such as countries, time zones, or associated records. option_tags is a string containing the option tags for the select box.

Options
:multiple - If set to true the selection will allow multiple choices.

:disabled - If set to true, the user will not be able to use this input.

Any other key creates standard HTML attributes for the tag.

select_tag "people", options_from_collection_for_select(@people, "name", "id")
# <select id="people" name="people"><option value="1">David</option></select>


[code]
---------------------------------
[b]select(object, method, choices, options = {}, html_options = {}) [/b]

Create a select tag and a series of contained option tags for the provided object and method. The option currently held by the object will be selected, provided that the object is available. See options_for_select for the required format of the choices parameter.

Example with @post.person_id => 1:

select("post", "person_id", Person.all.collect {|p| [ p.name, p.id ] }, { :include_blank => true })

[/code]

首先两者在传递参数上有点区别
select_tag传的就是一个参数(字符串)
select object/method 即对象和方法(属性)


options_from_collection_for_select(@people, "name", "id")
@people为对象货以对象为单元的数组,name为option的text值,id为option的value值

也可以用
options_for_select(Warehouse.find(:all).collect{|item|[item.name,item.id]}.insert(0,['请选择',0]))
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值