博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Rails中两种不同的表单处理方式
阅读量:4041 次
发布时间:2019-05-24

本文共 3336 字,大约阅读时间需要 11 分钟。

Rails中两种不同的表单处理方式
2009-05-14 12:34

1.表单类型一

<div class=""class="form">

<%= error_messages_for 'user' %>
<fieldset>
<legend>请输入用户信息</legend>
<% form_for :user do |form| %>
<p>
Name: <%= form.text_field :username, :size => 30 %>
</p>
<p>
Password: <%= form.password_field :password, :size => 30 %>
</p>
<p>
ConfirmPassword: <%= form.password_field :password_confirmation, :size => 30 %>
</p>
<%= submit_tag "注册", :class => "submit" %>
<% end %>
</fieldset>
</div>
此种是对象绑定的方式,通过表单,跟Model层的对象绑定,通常完成数据的增,改功能。

2.表单类型二

<div class=""class="form">

<%= error_messages_for 'user' %>
<fieldset>
<legend>请输入用户信息</legend>
<% form_tag do %>
<p>
Name: <%= text_field_tag :username, params[:username], :size => 30 %>
</p>
<p>
Password: <%= password_field_tag :password, params[:password], :size => 30 %>
</p>
<%= submit_tag "注册", :class => "submit" %>
<% end %>
</fieldset>
</div>
此种主要是为了表单传值
form_for和model绑定,而form_tag不是
form_tag想传什么参数都行,没有约束

==================[以下内容转自网络]===================================

表单开始标签:

<%= form_tag { :action => :save }, { :method => :post } %>
Use :multipart => true to define a Mime-Multipart form (for file uploads)
表单结束标签:
<%= end_form_tag %>

文本框 Text fields

<%= text_field :modelname, :attribute_name, options %>
生成:
<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="}" />

隐藏框:

<%= hidden_field ... %>

密码框:

<%= password_field ... %>

文件框

<%= 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]">
}
</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"

转载地址:http://qbadi.baihongyu.com/

你可能感兴趣的文章
Android 跨应用程序访问窗口知识点总结
查看>>
各种排序算法的分析及java实现
查看>>
SSH框架总结(框架分析+环境搭建+实例源码下载)
查看>>
js弹窗插件
查看>>
自定义 select 下拉框 多选插件
查看>>
js判断数组内是否有重复值
查看>>
js获取url链接携带的参数值
查看>>
gdb 调试core dump
查看>>
gdb debug tips
查看>>
arm linux 生成火焰图
查看>>
linux和windows内存布局验证
查看>>
linux insmod error -1 required key invalid
查看>>
linux kconfig配置
查看>>
linux不同模块completion通信
查看>>
linux printf获得时间戳
查看>>
C语言位扩展
查看>>
linux dump_backtrace
查看>>
linux irqdebug
查看>>
git 常用命令
查看>>
linux位操作API
查看>>