在Ruby on Rails中真的有一堆Select helper可以用,我们经常容易混淆。常见的有三个..
select, select_tag, collection_select(其余的什么select_date那些不谈)
我们先来看看一个基本的下拉式选项骨架

</p>
<select name="ROR">
<option value="">ROR1</option><br/>
<option value="">ROR2</option><br/>
<option value="">ROR3</option><br/>
</select>
<p>

在一个下拉式选项中,有一些是必备的信息,像”name”、”value”与”text”三个,在回传信息给Serve时,”name”将是接收信息用的,而”value”会传回被选的值,而”text”则是使用者会看到的字,依上面的例子来讲,ROR1、ROR2、ROR3就是属于”text”
开始讲讲哪三种Select helper

select:

select(object, method, choices, options = {}, html_options = {})


在ActionView::Helpers::FormOptionsHelper中定义

  • object事一个实体化变数,这里很明显的就是要摆上model物件嘛!
  • method则是object的一个属性,也是资料表中的对应项目
  • choices就是要被选的选项,可以事阵列或者事哈希(Hash)
  • options与html_options则是一些选项

来这里举个例子吧

<%= select("project", "teacher_id", @teachers.collect{|t| [t.name, t.id]}, { :include_blank => false })%>

<%= select("project", "student_id", {"CFC" => '', "EF" => ''}) %>
第一个例子中,@teachers在Controller是这样的
@teachers = Teacher.find(:all, :select => 'id, name')

select_tag:
select_tag(name, option_tags = nil, options = {})

在ActionView::Helpers::FormTagHelper中定义
如果你很喜欢动手打option的话.. 那用select_tag准没错啦!
在select_tag中,name将会是params所接收值所用的键
直接看范例

<%= select_tag 'user', "<option>CFC</option>" %>
  1. 这时在Controller中将会用params[:user]来接收传过来的值
  2. 但是select_tag也可以搭配options_for_select或者options_from_collection_for_select一起使用.. 来看一个范例吧
  3. <%= select_tag('sid[]', options_from_collection_for_select(@students, 'id', 'name'), :multiple =>true)%>
  4. 因为加上了:multiple,所以可以接受多值选择,这时在Controller接收到的sid将会是一个阵列,这也是我所卡住的地方..

collection_select:
collection_select(object, method, collection, value_method, text_method, options = {}, html_options = {})

在ActionView::Helpers::FormOptionsHelper中定义
如果信息来源是数据库的话,可以使用这个来做下拉式选项。
这个Object不用我说,就是你的model
method呢?当然就是栏目了
其实说起來,这只是select+options_from_collection_for_select的组合啦!
范例:

<%= collection_select(:payment, :id, @payments, :id, :name, options ={:prompt => "-Select a continent"}, :class =>"payment") %>

【转】Ruby on Rails中select使用方法的更多相关文章

  1. ruby on rails 中render的

    Ruby rails页面跳转代码如下: 1.render(:text => string) 2.render(:inline => string, [:type => "r ...

  2. 理解ruby on rails中的ActiveRecord::Relation

    ActiveRecord::Relation是rails3中添加的.rails2中的finders, named_scope, with_scope 等用法,在rails3统一为一种Relation用 ...

  3. Ruby on Rails中的Rake教程(Rake如何把我灌醉!)

    下面是我们使用Rake任务的例子: 1.给列表中的用户发送邮件 2.每晚数据的计算和报告 3.过期或重新生成缓存 4.备份数据和svn版本(how's this : subversion reposi ...

  4. json格式在ruby和rails中的注意事项

    #虚拟网络拓扑的json数据 def topodata #@vnic = Vnic.all #flash.now[:notice] = 'Message sent!' #flash.now[:aler ...

  5. rails中validates_confirmation_of验证方法无效的解决办法

    rails的model中提供了很多种自带的验证方法,validates_confirmation_of可以验证变量xxx和xxx_confirmation是否相等:这可以用于验证2遍输入的密码是否一致 ...

  6. ruby on rails 中render的使用

    最近写ror,因为比较菜,很多东西不知道,只能看一点查一点了 render 先上点搜集的常用方式 render :action => "long_goal", :layout ...

  7. rails中select不能响应多选的解决办法

    在rails4.2中如果你写如下代码,post的select无法传回多选内容,即使你select设置为多选: <select id='id_size' name='name_size' mult ...

  8. linux c中select使用方法

    1.select函数作为定时器使用    it_value.tv_sec = 0;    it_value.tv_usec = 100000:    select(1,NULL,NULL,NULL,& ...

  9. Ruby on Rails 中你使用了Kaminari 后,千万不要再引入will_pagination 这个Gem 了

    今日做开发的时候发现的这个问题 发现无论怎样配置都不能使用Kaminari 的Per 这个功能,分页大小也固定在了30 最开始还以为是Ransack 这个Gem 影响的,上网搜了很久发现没有 最后仔细 ...

随机推荐

  1. You can also run `php --ini` inside terminal to see which files are used by PHP in CLI mode.

    [root@localhost dreamstart]# composer installLoading composer repositories with package informationI ...

  2. jauery table

    $("#tableData tr:gt(0)").each(function() { }//橘色部分是查找id为tableData的DataTable里面除第一行以外的行

  3. ssm框架与shiro的整合小demo,用idea开发+maven管理

    shiro安全框架是目前为止作为登录注册最常用的框架,因为它十分的强大简单,提供了认证.授权.加密和会话管理等功能 . shiro能做什么? 认证:验证用户的身份 授权:对用户执行访问控制:判断用户是 ...

  4. 云计算服务的三种类型(SaaS、PaaS、IaaS)

    云计算可以帮助企业降低IT方面的成本和复杂性,并获得他们蓬勃发展所需的灵活性与敏捷性.但是,规划出通往云的明确路径并非易事.毕竟用户需要看透与云相关的市场大肆宣传,然后理解并分析不同种类的云计算模式的 ...

  5. spring 注解事务

    前提:在applicationContext.xml中配置<tx:annotation-driven transaction-manager="transactionManager&q ...

  6. 【leetcode刷题笔记】Subsets

    Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be ...

  7. Vue-Quill-Editor回显不显示空格的处理办法

    我自己在用VUE做一个博客网站,查了一下好多人都在用Vue-Quill-Editor,于是我也把这个插件加入到我的程序里,可是后来却出现了个问题,如图: 简单的代码如下: <template&g ...

  8. 算法(Algorithms)第4版 练习 2.2.11(2)

    关键代码: private static void sort(Comparable[] input, int lo, int hi) { if(lo >= hi)//just one entry ...

  9. static_cast, dynamic_cast, const_cast 三种类型转化的区别

    强制转化四种类型可能很多人都常常忽略就象我一样,但是有时还是比较有用的.不了解的建议看看,一些机制我也不是十分了解,只是将一些用法写出来让大家看看.                           ...

  10. Codeforces Round #250 (Div. 2) D. The Child and Zoo 并查集

    D. The Child and Zoo time limit per test 2 seconds memory limit per test 256 megabytes input standar ...