简体   繁体   English

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

[英]How to use both blank and default date in date_select in Rails?

The following code works as expected, using the @the_date as the default selected date. 以下代码按预期工作,使用@the_date作为默认选定日期。

<%= date_select("the_date", "", :default => @the_date ) %>

However, in the case there is no date, it needs to show blanks. 但是,如果没有日期,则需要显示空白。 The following code displays blanks, but also forces the default selection to be blank even when the date is defined. 以下代码显示空白,但即使定义了日期,也会强制默认选择为空白。

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

How do you set the default selection to a date, and to blank if the date is nil? 如何将默认选择设置为日期,如果日期为零则将其设置为空白?

Working in Rails 3.07. 在Rails 3.07中工作。

Thanks. 谢谢。

Rails ignores your :default if you also use either :include_blank or :prompt . 如果你还使用:include_blank:prompt Rails会忽略你的:default

IMO this is a bug but there we go. IMO这是一个错误,但我们去了。

Just, :include_blank => @the_date.nil? 只是,:include_blank => @ the_date.nil? , it should do the trick. ,它应该做的伎俩。 See example below 见下面的例子

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM