如何在Rails中的date_select中使用空白和默认日期?

B S*_*ven 3 ruby-on-rails form-helpers ruby-on-rails-3

以下代码按预期工作,使用@the_date作为默认选定日期.

<%= date_select("the_date", "", :default => @the_date ) %>
Run Code Online (Sandbox Code Playgroud)

但是,如果没有日期,则需要显示空白.以下代码显示空白,但即使定义了日期,也会强制默认选择为空白.

<%= date_select("patient_details[birth_date]", "", { :default => @the_date, :include_blank => true } ) %>
Run Code Online (Sandbox Code Playgroud)

如何将默认选择设置为日期,如果日期为零则将其设置为空白?

在Rails 3.07中工作.

谢谢.

And*_*art 10

Rails的忽略你:default,如果你也使用两种:include_blank或:prompt.

IMO这是一个错误,但我们去了.


Ris*_*ogi 9

只是,:include_blank => @ the_date.nil?,它应该做的伎俩.见下面的例子

date_select(:patient_details, :birth_date, { :default => @the_date, :include_blank=> @the_date.nil? })

  • 只是?它有效,但输入日期后无法选择空白.如何处理用户想要删除现有日期的用例? (2认同)