Greg
Greg

Reputation: 2609

Date in Rails form

I have a form field in my Rails view:

<%= f.date_select :Date_of_Birth %>

This shows a drop down menu with only the last 10 years and I need to go back to 1800. Better if I could just type the year in rather than have a 200 item drop down list. I also need the option not to fill in the date, that is, blank.

I have found a couple of hints here in stackoverflow, but they don't use quite this format which was generated for me. Suspect because I'm using Rails 4.

Upvotes: 1

Views: 4016

Answers (1)

sled
sled

Reputation: 14625

Here's how to extend the range of selection:

<%= f.date_select :Date_of_Birth, start_year: 1800, end_year: Time.now.year %>

Having a text input field for the year is not so easy but it could be done, by adding discard_year: true to the date_select.

Then you add a text field tag AFTER the date_select inputs with a special name like name_of_your_object_Date_of_Birth(1i) where "name_of_your_object" is the name you used in the form_for tag. Inspect the HTML fields which date_selects generates in your browser.

Upvotes: 5

Related Questions