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)%>
相关文档:
#一、定义一个类
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 ......
Beginning Ruby from Novice To Perfessional
Head First Rails
Agile Web Development with Rails 3rd Edition(new)
Agile Web Development with Rails 3rd Edition
Agile Web Development with Rails 3rd Editio ......
phpRPC + Ruby + Arduino = 遠程控制LED開關(?)
嗯,我知道這是個很無聊的Sample :P
關於phpRPC與Arduino請自行Google
觀看此demo之前請先安裝另外一篇所提到的serialport
與另外一個Gem
套件:phprpc
在這個ļ ......
To get it done is not easy. I spent a whole day to figure out the various compatibility issues along the way out.
Now there still might be potential issues, but it works by my rough test.
Step 1: Install Apache Cassandra
You may know that the Ruby gem cassandra will do it for you.
  ......
rubygems
Ruby on Rails 是一个可以使你开发,部署,维护 web 应用程序变得简单的框架。
Ruby的作者于1993年2月24日开始编写Ruby,直至1995年12月才正式公开发布于fj(新闻组)。之所以称为Ruby,是因为Perl的发音与6月的诞生石pearl(珍珠)相同,因此Ruby以7月的诞生石ruby(红宝石)命名。rails在英文中是栏杆的 ......