B Seven
B Seven

Reputation: 45943

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.

<%= 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.

Thanks.

Upvotes: 3

Views: 5435

Answers (2)

Andy Stewart
Andy Stewart

Reputation: 5498

Rails ignores your :default if you also use either :include_blank or :prompt.

IMO this is a bug but there we go.

Upvotes: 11

Rishav Rastogi
Rishav Rastogi

Reputation: 15492

Just, :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? })

Upvotes: 9

Related Questions