rails中的form表单总结
Rails中两种不同的表单处理方式
1.表单类型一
<div %>
<% end %>
</fieldset>
</div>
此种是对象绑定的方式,通过表单,跟Model层的对象绑定,通常完成数据的增,改功能。
2.表单类型二
<div %> 实例: 隐藏框: 密码框: 文件框 Rails Textarea框 单选框 Radio Buttons 多选框 Check Box check_box "puppy", "gooddog", {}, "yes", "no" <%= select :variable, :attribute, choices, options, html_options %> 下拉菜单框 Select Menu Collection Selection 日期选择框: datetime_select "post", "written_on"
<% end %>
</fieldset>
</div>
此种主要是为了表单传值
form_for和model绑定,而form_tag不是
text_field "post", "title", "size" => 20
<input type="text" id="post_title" name="post[title]"
<%= hidden_field ... %>
<%= password_field ... %>
<%= file_field ... %>
<%= 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_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]"<input type="radio" id="post_category" name="post[category]"
<%= check_box :modelname, :attribute, options, on_value, off_value<input type="checkbox" id="post_validate" name="post[validated]"<input name="post[validated]" type="hidden"
<input type="checkbox" id="puppy_gooddog" name="puppy[gooddog]"<input name="puppy[gooddog]" type="hidden"
select "post",<option?value="2">Sam</option>
<option?value="3">Tobias</option>
</select>
<%= 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]