Rails 3 - How to Set Start Date to Date_Select Method

Rails 3 - Can I set start date to date_select method?

The date_select helper only supports setting a start_year and end_year (i.e. 2015-2020), not a start_date and end_date (i.e. June 1st, 2015 to July 1st, 2020). The date_select helper is design to be very simple. If you look closely, you will even notice every month has 31 days.

A few workarounds are available:

  1. Do validations on the server side and present errors if dates are submitted that are outside an acceptable range.
  2. Switch to using a JavaScript client - such as http://amsul.ca/pickadate.js/date/ - that allows limiting the start and end (should still use server side validations as well).

Rails 3 date_select for year only

Rails has a select_year helper:

http://apidock.com/rails/ActionView/Helpers/DateHelper/select_year

So your code should look like:

f.select_year(Date.today, :start_year => Time.now.year, :end_year => Time.now.year - 95, :field_name => 'grad_year')

In rails how to set a start date and an end date for date form fields?

From the Rails date_select official documentation:

:start_year - Set the start year for the year select. Default is Date.today.year - 5 if you are creating new record. While editing existing record, :start_year defaults to the current selected year minus 5.

I'll leave to you the exercise of reading the documentation for date_field or any other method.

date select showing days before and after given date

Put simply: you can't do it with a date_select. You would need to build it using either the select or collection_select helpers and work out the values for the dates yourself. There is no helper in Rails to do that as far as I am aware.

Rails - set date_select in form 18 years ago

Try

Time.now.year - 18

or

Date.today.year - 18

form_tag with date_select in rails3

Have a look at select_date:

http://apidock.com/rails/ActionView/Helpers/DateHelper/select_date

It has the form:

select_date(date = Date.current, options = {}, html_options = {})

How can I set just the year and month for date_select for HAML and Rails 3?

You should have a look at this: http://api.rubyonrails.org/classes/ActionView/Helpers/DateHelper.html#method-i-date_select

date_select(object_name, method, options = {}, html_options = {})

There should be the method as second parameter and then the options.
Also I found on the documentation the option :discard_day, which doesn't display the select for the day.



Related Topics



Leave a reply



Submit