Rails 表单简单使用

来源:互联网 发布:淘客微信公众号源码 编辑:程序博客网 时间:2024/06/05 06:43


表单开始标签:
Ruby代码 收藏代码

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

表单结束标签:
Ruby代码 收藏代码

<%= end_form_tag %>

文本框 Text fields
Ruby代码 收藏代码

<%= text_field :modelname, :attribute_name, options %>

生成:
Ruby代码 收藏代码

<input type="text" name="modelname[attribute_name]" id="attributename" />

实例:

Ruby代码 收藏代码

text_field "post", "title", "size" => 20
<input type="text" id="post_title" name="post[title]"
size="20" value="#{@post.title}" />

隐藏框:
Ruby代码 收藏代码

<%= hidden_field ... %>

密码框:
Ruby代码 收藏代码

<%= password_field ... %>

文件框
Ruby代码 收藏代码

<%= file_field ... %>

Rails Textarea框
Ruby代码 收藏代码

<%= text_area ... %>

实例:
Ruby代码 收藏代码

text_area "post", "body", "cols" => 20, "rows" => 40
<textarea cols="20" rows="40" id="post_body" name="post[body]">
#{@post.body}
</textarea>

单选框 Radio Buttons
Ruby代码 收藏代码

<%= radio_button :modelname, :attribute, :tag_value, options %>

实例:
Ruby代码 收藏代码

radio_button "post", "category", "rails"
radio_button "post", "category", "ruby"
<input type="radio" id="post_category" name="post[category]" value="rails"
checked="checked" />
<input type="radio" id="post_category" name="post[category]" value="ruby" />

多选框 Check Box
Ruby代码 收藏代码

<%= check_box :modelname, :attribute, options, on_value, off_value %>

实例
Ruby代码 收藏代码

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
Ruby代码 收藏代码

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 %>

实例:
Ruby代码 收藏代码

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"
表单:

ajax表单:

<% form_remote_tag :url => { :action => 'register' } do %>

.................

<% end %>

或者

<%= form_remote_tag(:url => { :action => 'forgot' }) %>

.........................

<%= form_tag %>

传统表单方式:

<% form_tag({:controller=>"*",:action=>"*"},:name=>"searchform",:method=>"post",:multipart=>"true"}) do %>

<% end %>

文本框:

(1),用户名:<%= text_field 'user','name' , :size=>20 %> 等同于 <%= text_field :user,:name, :size=>20 %>

controller中取值:params[:user][:name]

(2)组名:<%= text_field_tag :name,@group_name %>

controller中取值 params[:name],@group_name是默认值,可以从controller中传值到这里(一般是把form放到partial中)

密码框:
密码:<%= password_field 'user','password' ,:size=>20 %>

controller中取值 params[:user][:password]

文本区:

组备注: <%= text_area_tag :comment,@group_comment %>

controller中取值:params[:comment],默认值@group_comment

单选按钮:

是否收费:<%= radio_button_tag 'fee','1', false %> 是 <%= radio_button_tag 'fee','0',false %> 否

controller中取值: params[:fee]

下拉列表:

(1),状态: <%= select_tag :trend, options_for_select([["正常", "1"],
["禁用", "0"]],
[@status, @status_value]) %>

controller中取值 params[:trend],@status是显示的值,@status_value是传出去的值

(2),

<%= select_tag 'charge', options_from_collection_for_select(@charge,'price','level',1) %>

@charge是从controller中传过来的值集合

发送表单:

<%= submit_tag '添加',{ :name=>'newgroup' } %>

后面的{}内是name属性值 例如可以再controller中取值 params[:newgroup] 得出的值是 : 添加

注意点:

1,rhtml中可以使用原始方式 不信你查看下页面源码就知道了,例如:

(1),<label><input type="submit" name="newgroup" value="添加" /></label>

这个和上面的发送表单效果一样

(2),但是使用这种方式有时在不同的浏览器里会有bug,例如:

<label>是否收费: &nbsp;&nbsp; <input type="radio" name="fee" value = "1">是
<input type="radio" name="fee" value = "0">否<br>
</label>

在ie,opera,chrome都可以正常使用,但是在FF中就不可以使用,所以建议还是使用rails自带的方式书写

2,一般rails自带的都有两种书写方式,后期出现了*_tag的方法,就如上面的 文本框 但是建议用 *_tag的方式表达

3,'user' 和 :user 的都是参数的表示的方式,但是建议使用后者,这样更符合rails的规范写法!

0 0