2009-02-18 18 views
10

對於「新」對象表單使用<%= f.datetime_select:start%>時,如何設置選項默認情況下不選擇日期,而是使用nil/null值?相關模型中的字段可以有選擇地爲null,但是默認情況下,datetime_select控件沒有null選項。datetime_select沒有值?

回答

27

我想你想:

f.datetime_select :start, :include_blank => true

start爲零,字段將選擇空白選項。這也允許用戶而不是輸入日期。

有關詳細信息,請參閱the documentation for date_select

0

您將要使用:prompt選項。

從Rails的文檔:

# Generates a datetime select with a custom prompt. Use :prompt=>true for generic prompts. 
    datetime_select("post", "written_on", :prompt => {:day => 'Choose day', :month => 'Choose month', :year => 'Choose year'}) 
    datetime_select("post", "written_on", :prompt => {:hour => true}) # generic prompt for hours 
    datetime_select("post", "written_on", :prompt => true) # generic prompts for all 
+0

擴展樣本似乎沒有工作。 <%= f.datetime_select:start,:prompt => {:day =>'選擇日期',:month =>'選擇月份',:year =>'選擇年份'}%> 是否存在語法錯誤這裏? – 2009-02-18 03:40:21

+0

你可以使用香草助手:<%= datetime_select:my_object,:start,:prompt => {:day =>'Choose day',:month =>'選擇月份',:year =>'選擇年份'} %> – 2009-02-18 11:37:15