`
JasonChi
  • 浏览: 92188 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

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>


---------------------------------
[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 })



首先两者在传递参数上有点区别
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]))
分享到:
评论
1 楼 hxz_qlh 2011-08-08  
楼主在杭州哪个公司搞rails 开发?

相关推荐

Global site tag (gtag.js) - Google Analytics