2008-01-17
[转载]Rails常用表单
关键字: rails, ror
为了方便对rhtml中表单的学习和使用,整理了一部分rails常用表单用法。
表单开始标签:
<%= form_tag { :action => :save }, { :method => :post } %>
#开始一个表单,相当于
#html表单<input type="form" action="save",method="post">
Use :multipart => true to define a Mime-Multipart form (for file uploads)
#定义一个Mime-Multipart格式的表单,用于文件上传
表单结束标签:
<%= end_form_tag %>
文本框 Text fields
<%= text_field :modelname, :attribute_name, options%>
#这个是最常用的文本输入框,三个参数依次是模型名,模型的属性名,以及选项,诸如size,value等文本框属性。
生成:
<input type="text" name="modelname[attribute_name]" id="attributename" />
实例:
text_field "post", "title", "size" => 20
<input type="text" id="post_title" name="post[title]"
size="20" value="#{@post.title}" />
隐藏框:
<%= hidden_field ... %>
#隐藏表单,类似<input type="hidden"..>
密码框:
<%= password_field ... %>
#类似<input type="password"..>
文件框:
<%= file_field ... %>
Rails Textarea框:
<%= text_area ... %>
实例:
text_area "post", "body", "cols" => 20, "rows" => 40
<textarea cols="20" rows="40" id="post_body" name="post[body]">
#{@post.body}
</textarea>
单选框 Radio Buttons
<%= radio_button :modelname, :attribute, :tag_value, options %>
实例:
radio_button "post", "category", "rails"
radio_button "post", "category", "java"
<input type="radio" id="post_category" name="post[category]" value="rails"
checked="checked" />
<input type="radio" id="post_category" name="post[category]" value="java" />
多选框 Check Box
<%= check_box :modelname, :attribute, options, on_value, off_value %>
实例:
check_box "post", "validated" # post.validated? returns 1 or 0
<input type="checkbox" id="post_validate" name="post[validated]"
value="1" checked="checked" />
<input name="post[validated]" type="hidden" value="0" />
check_box "puppy", "gooddog", {}, "yes", "no"
<input type="checkbox" id="puppy_gooddog" name="puppy[gooddog]" value="yes" />
<input name="puppy[gooddog]" type="hidden" value="no" />
<%= select :variable, :attribute, choices, options, html_options %>
下拉菜单框 Select Menu
select "post",
"person_id",
Person.find_all.collect {|p| [ p.name, p.id ] },
{ :include_blank => true }
<select name="post[person_id]">
<option></option>
<option value="1" selected="selected">David</option>
<option value="2">Sam</option>
<option value="3">Tobias</option>
</select>
Collection Selection
<%= collection_select :variable, :attribute, choices, :id, :value %>
日期选择框:
<%= date_select :variable, :attribute, options %>
<%= datetime_select :variable, :attribute, options %>
实例:
date_select "post", "written_on"
date_select "user", "birthday", :start_year => 1910
date_select "user", "cc_date", :start_year => 2005,
:use_month_numbers => true,
:discard_day => true,
:order => [:year, :month]
datetime_select "post", "written_on"
表单开始标签:
<%= form_tag { :action => :save }, { :method => :post } %>
#开始一个表单,相当于
#html表单<input type="form" action="save",method="post">
Use :multipart => true to define a Mime-Multipart form (for file uploads)
#定义一个Mime-Multipart格式的表单,用于文件上传
表单结束标签:
<%= end_form_tag %>
文本框 Text fields
<%= text_field :modelname, :attribute_name, options%>
#这个是最常用的文本输入框,三个参数依次是模型名,模型的属性名,以及选项,诸如size,value等文本框属性。
生成:
<input type="text" name="modelname[attribute_name]" id="attributename" />
实例:
text_field "post", "title", "size" => 20
<input type="text" id="post_title" name="post[title]"
size="20" value="#{@post.title}" />
隐藏框:
<%= hidden_field ... %>
#隐藏表单,类似<input type="hidden"..>
密码框:
<%= password_field ... %>
#类似<input type="password"..>
文件框:
<%= file_field ... %>
Rails Textarea框:
<%= text_area ... %>
实例:
text_area "post", "body", "cols" => 20, "rows" => 40
<textarea cols="20" rows="40" id="post_body" name="post[body]">
#{@post.body}
</textarea>
单选框 Radio Buttons
<%= radio_button :modelname, :attribute, :tag_value, options %>
实例:
radio_button "post", "category", "rails"
radio_button "post", "category", "java"
<input type="radio" id="post_category" name="post[category]" value="rails"
checked="checked" />
<input type="radio" id="post_category" name="post[category]" value="java" />
多选框 Check Box
<%= check_box :modelname, :attribute, options, on_value, off_value %>
实例:
check_box "post", "validated" # post.validated? returns 1 or 0
<input type="checkbox" id="post_validate" name="post[validated]"
value="1" checked="checked" />
<input name="post[validated]" type="hidden" value="0" />
check_box "puppy", "gooddog", {}, "yes", "no"
<input type="checkbox" id="puppy_gooddog" name="puppy[gooddog]" value="yes" />
<input name="puppy[gooddog]" type="hidden" value="no" />
<%= select :variable, :attribute, choices, options, html_options %>
下拉菜单框 Select Menu
select "post",
"person_id",
Person.find_all.collect {|p| [ p.name, p.id ] },
{ :include_blank => true }
<select name="post[person_id]">
<option></option>
<option value="1" selected="selected">David</option>
<option value="2">Sam</option>
<option value="3">Tobias</option>
</select>
Collection Selection
<%= collection_select :variable, :attribute, choices, :id, :value %>
日期选择框:
<%= date_select :variable, :attribute, options %>
<%= datetime_select :variable, :attribute, options %>
实例:
date_select "post", "written_on"
date_select "user", "birthday", :start_year => 1910
date_select "user", "cc_date", :start_year => 2005,
:use_month_numbers => true,
:discard_day => true,
:order => [:year, :month]
datetime_select "post", "written_on"
- 15:44
- 浏览 (2333)
- 评论 (0)
- 分类: Ruby&Rails
- 相关推荐
发表评论
- 浏览: 33441 次
- 性别:

- 来自: 北京

- 详细资料
搜索本博客
最近加入圈子
最新评论
-
痛苦的表设计 --- rails未 ...
这个事情说明了,项目一定要有个架构师负责整体架构设计。
-- by BirdGu -
痛苦的表设计 --- rails未 ...
在这种项目管理混乱,谁都可以捣浆糊的地方,楼主责无旁贷的背起了黑锅
-- by younggun -
痛苦的表设计 --- rails未 ...
rails的思想就是,“我的就是最简单的,我并不想做一个复杂无比包解决万事的超级 ...
-- by seemoon -
痛苦的表设计 --- rails未 ...
郁闷,建立模型关系真是犯难
-- by shaka -
痛苦的表设计 --- rails未 ...
我也在做一个项目使用多种语言开发, 后台是C,JAVA, web使用php , ...
-- by meng9999






评论排行榜