Ruby on Rails中select使用方法
在Ruby on Rails中真的有一堆Select helper可以用,我们经常容易混淆。常见的有三个..
select, select_tag, collection_select(其余的什么select_date那些不谈)
我们先来看看一个基本的下拉式选项骨架
</p>
<select
name="ROR">
<option
value="1">ROR1</option><br
/>
<option
value="2">ROR2</option><br
/>
<option
value="3">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" => '1', "EF" => '2'}) %>
第一个例子中,@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>" %>
这时在Controller中将会用params[:user]来接收传过来的值
但是select_tag也可以搭配options_for_select或者options_from_collection_for_select一起使用.. 来看一个范例吧
<%= select_tag('sid[]', options_from_collection_for_select(@students, 'id', 'name'), :multiple => true)%>
相关文档:
学了一个学期的C语言,看了一个星期的ruby,我才发现为什么老师说C是最基础的,假如没有一个学期的C基础,那ruby我也不用看了。
Ruby和C语言有许多的相同点和不同点,在学习ruby时,有时可以用C里面的思维来理解,就像ruby里面的方法其实就跟C的函数如出一 ......
历史背景[1]
诞生于1993年2月24日的Ruby(红宝石),由日本人松本行弘(Yuki Matsumoto)于1994年12月发布。 Ruby 作为一种纯面向对象的脚本程序设计语言,吸收了Smalltalk和Perl两种程序语言的特性。
Ruby理念[2]
1.首先考虑的是,减少编程时不必要的琐碎时间,令编写程序的人高兴;
2.其次是良好的界面设计;
3.强 ......
1,安装ruby
下载地址: http://rubyinstaller.org/download.html
2,安装rails
使用命令下载:在命令行运行gem install rails --remote
2,初步了解ruby的文章
http://www.bcbbs.net/html/29671.html
http://www.bcbbs.net/dev/List64.aspx ......
转 Adding Sound to Your Ruby Apps
Have you ever thought about including sounds in your Ruby application? Used sparingly, sound may enhance your applications by adding audio cues or a custom touch. You could, for example, play a beep or chime that announces the completion of a lengthy process. Per ......
self上下文
Ruby的self有和Java的this相似之处,但又大不相同。Java的方法都是在实例方法中引用,所以this一般都是指向当前对象的。而Ruby的代码逐行执行,所以在不同的上下文(context)self就有了不同的含义,先来看看常见的context self都代表哪些
1
2
3
4
5
6
7
8
9
10
11
12
13
......