rails表单控件helper

1、form加入HTML属性

<%= form_for(@device, :html => {:method=>"post", :id=>"form1", :name=>"form1", :target=>"__hidden_call"}) do |f| %>
<% form_for :person, @person, :url => { :action => "update" } %>

<%= form_tag({:action => "upload_sand_score"}, multipart: true) do |f| %>

 2、link_to

<%= link_to "Issues", {:controller => "issue", :action => "index"}, {:onclick => "alert(1)"} %> 
<%= link_to "Issues", {:controller => "issue", :action => "index", :param=value}, :onclick => "alert(1)" %> #加参数

 3、select

#(1)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')

#(2)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)%>
#因为加上了:multiple,所以可以接受多值选择,这时在Controller接收到的sid将会是一个阵列,这也是我所卡住的地方..

#(3)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") %>

 

posted @ 2014-08-19 17:33  zhangsai  阅读(333)  评论(0编辑  收藏  举报