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)%>
相关文档:
操作系统ubuntu,开发工具netbeans
vendor数据库表有字段service_category varchar(100)
<% form_for :vendor, @vendor, :url => sellers_path do |f| %>
<%= f.select(:service_category,
  ......
#一、定义一个类
class Person
def initialize(name,age=18)
@name=name;
@age=age;
@motherland="china";
end
def talk
puts "my name is "+@name+" and I am "+@age.to_s
&nb ......
When I try the command "gem install thrift" with Ruby 1.9.1, I got a compilation error with something related to a C function "strlcpy()".
Then I searched the web. It seems I am not alone and the community know it.
However I do not want to wait for official update, I want to ......
历史背景[1]
诞生于1993年2月24日的Ruby(红宝石),由日本人松本行弘(Yuki Matsumoto)于1994年12月发布。 Ruby 作为一种纯面向对象的脚本程序设计语言,吸收了Smalltalk和Perl两种程序语言的特性。
Ruby理念[2]
1.首先考虑的是,减少编程时不必要的琐碎时间,令编写程序的人高兴;
2.其次是良好的界面设计;
3.强 ......
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
......