2012-08-22 57 views
3

我正在對網站進行可訪問性檢查,並且遇到了一個問題,我不知道如何在Rails中解決問題。代碼如下:向rails添加標籤date_select helper

<%= f.label :birthdate %> 
<%= f.date_select :birthdate, {:start_year => Time.now.year - 14, :end_year => 1900, :prompt => true, :order => [:day, :month, :year]} %> 

主要生產:

<label for="birthdate">Birthdate</label> 
<select id="birthdate_3i" name="birthdate(3i)"> 
    <option value="">Day</option> 
    <option value="1">1</option> 
     ... 
    <option value="31">31</option> 
</select> 
<select id="birthdate_2i" name="birthdate(2i)"> 
    <option value="">Month</option> 
    <option value="1">January</option> 
     ... 
    <option value="12">December</option> 
</select> 
<select id="birthdate_1i" name="birthdate(1i)"> 
    <option value="">Year</option> 
    <option value="1998">1998</option> 
     ... 
    <option value="1900">1900</option> 
</select> 

有誰知道,如果我必須手動寫標籤/字段集對於已經產生的3個選擇字段?或者我應該不同地使用rails代碼。我對鐵軌有點新...更多的是前鋒。在

回答

0
date_select("DOB", :birthdate, :prompt => { 
:day => 'Select day', :month => 'Select month', :year => 'Select year', 
:start_year => Time.now.year - 14, :end_year => 1900 
}) 

更多選擇:
http://api.rubyonrails.org/classes/ActionView/Helpers/DateHelper.html#method-i-date_select

+1

感謝邁克爾。我現在看到「提示」選項的作用。但我真正追求的是爲製作的選擇框添加標籤。我不確定它是否可行,所以我認爲我只是將它包裝在帶有圖例的字段集中。 – johngeorgewright

0

我不認爲這是可能產生單獨的標籤。即使你願意爲一個標籤付出代價,還有一個額外的麻煩,它不是嚴格有效的HTML,因爲沒有「生日」表單控件。你是否應該在標籤助手中使用:birthdate_3i?

0

只是包裝的date_selectdivfieldset。標籤可以指向具有ID的任何東西,並且如果您有多個輸入饋送到一個數據類型(如軌道date_select),那麼您可以標記容器,並將選擇框作爲其組件。

<%= f.label :birthdate %> 
<div id="birthdate"> 
    <%= f.date_select :birthdate, {:start_year => Time.now.year - 14, :end_year => 1900, :prompt => true, :order => [:day, :month, :year]} %> 
</div> 
0

我最近雨水進入這個在一個項目上,我們發現,只有這樣,才能在Rails日期選擇每個select之前添加標籤是猴子補丁軌。我們在Rails 5.1.4上。這是建立在月/日/年選擇當date_separator作爲參數傳遞把一個分離器的最後2個選擇之前的原始方法:

https://github.com/rails/rails/blob/2c97fbf6503c9199f3fe5ed06222e7226dc6fcd9/actionview/lib/action_view/helpers/date_helper.rb#L1091

這裏是我們的猴子補丁,把一個傳說分離每一個日期之前與正確for標籤選擇:

module ActionView 
    module Helpers 
    class DateTimeSelector 

     # Given an ordering of datetime components, create the selection HTML 
     # and join them with their appropriate separators. 
     def build_selects_from_types(order) 
     select = "" 
     order.reverse_each do |type| 
      separator = separator(type) 
      select.insert(0, separator.to_s + send("select_#{type}").to_s + "</div>") 
     end 
     select.html_safe 
     end 

     # Returns the separator for a given datetime component. 
     def separator(type) 
     return "" if @options[:use_hidden] 
     field_name = "#{@options[:prefix]}_#{@options[:field_name]}" 
     case type 
      when :year 
      "<label for='#{field_name}_1i'>Year</label>" 
      when :month 
      "<label for='#{field_name}_2i'>Month</label>" 
      when :day 
      <label for='#{field_name}_3i'>Day</label>" 
      when :hour 
      (@options[:discard_year] && @options[:discard_day]) ? "" : @options[:datetime_separator] 
      when :minute, :second 
      @options[:"discard_#{type}"] ? "" : @options[:time_separator] 
     end 
     end 
    end 
    end 
end