1.表单类型一
<%= error_messages_for 'user' %>
请输入用户信息
<% form_for :user do |form| %>
Name: <%= form.text_field :username, :size => 30 %>
Password: <%= form.password_field :password, :size => 30 %>
ConfirmPassword: <%= form.password_field :password_confirmation, :size => 30 %>
<%= submit_tag "注册", :class => "submit" %>
<% end %>
此种是对象绑定的方式,通过表单,跟Model层的对象绑定,通常完成数据的增,改功能。
2.表单类型二
<%= error_messages_for 'user' %>
请输入用户信息
<% form_tag do %>
Name: <%= text_field_tag :username, params[:username], :size => 30 %>
Password: <%= password_field_tag :password, params[:password], :size => 30 %>
<%= submit_tag "注册", :class => "submit" %>
<% end %>
此种主要是为了表单传值
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 %>
生成:
实例:
text_field "post", "title", "size" => 20
size="20" value="#{@post.title}" />
隐藏框:
<%= hidden_field ... %>
密码框:
<%= password_field ... %>
文件框
<%= file_field ... %>
Rails Textarea框
<%= text_area ... %>
实例:
text_area "post", "body", "cols" => 20, "rows" => 40
单选框 Radio Buttons
<%= radio_button :modelname, :attribute, :tag_value, options %>
实例:
radio_button "post", "category", "rails"
radio_button "post", "category", "java"
checked="checked" />
多选框 Check Box
<%= check_box :modelname, :attribute, options, on_value, off_value %>
实例
check_box "post", "validated" # post.validated? returns 1 or 0
value="1" checked="checked" />
check_box "puppy", "gooddog", {}, "yes", "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 }
David
Sam
Tobias
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"