Ruby on Rails - HTML 表单



表单

要创建具有指定操作和 POST 请求的表单标签,请使用以下语法:

<%= form_tag :action => 'update', :id => @some_object %>

<%= form_tag( { :action => :save, }, { :method => :post }) %>

使用 :multipart => true 定义 MIME-multipart 表单(用于文件上传)。

<%= form_tag( {:action => 'upload'}, :multipart => true ) %>

文件上传

在你的视图中定义一个 multipart 表单:

<%= form_tag( { :action => 'upload' }, :multipart => true ) %>
   Upload file: <%= file_field( "form", "file" ) %>
   <br />
   
   <%= submit_tag( "Upload file" ) %>
<%= end_form_tag %>

在控制器中处理上传:

def upload
   file_field = @params['form']['file'] rescue nil
   
   # file_field is a StringIO object
   file_field.content_type # 'text/csv'
   file_field.full_original_filename
   ...
end

文本字段

要创建文本字段,请使用以下语法:

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

请看以下示例:

<%= text_field "person", "name", "size" => 20 %>

这将生成以下代码:

<input type = "text" id = "person_name" name = "person[name]" 
   size = "20" value = "<%= @person.name %>" />

要创建隐藏字段,请使用以下语法:

<%= 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]" 
   value = "rails" checked = "checked" />
<input type = "radio" id = "post_category" name = "post[category]" value = "java" />

复选框

要创建复选框,请使用以下语法:

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

请看以下示例:

check_box("post", "validated")

这将生成以下代码:

<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("post", "person_id", Person.find(:all).collect {|p| [ p.name, p.id ] })

这可能会生成以下代码。这取决于你的数据库中有哪些值:

<select name = "post[person_id]">
   <option value = "1">David</option>
   <option value = "2">Sam</option>
   <option value = "3">Tobias</option>
</select>

日期时间

以下是使用日期和时间的语法:

<%= 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> 标签:

<%= end_form_tag %>
rails-references-guide.htm
广告