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:
- Do validations on the server side and present errors if dates are submitted that are outside an acceptable range.
- 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
Rails 4 How to Call Accessible_Attributes from Model
Error Installing SQLite3 Gem via Bundler
How to Execute Windows Cli Commands in Ruby
Obtaining a Facebook Auth Token for a Command-Line (Desktop) Application
How to Stop Rails' Built-In Server from Listening on 0.0.0.0 by Default
Can One Yaml Object Refer to Another
How to Do Ruby Object Serialization Using JSON
Undefined Method 'Group_By_Day' - Rails 3.2
Gem Install Cannot Find a Header File
How to Specify the Location of the Chromedriver Binary
Cocoon Add Association, How to Limit Number of Associations
Inherit Class-Level Instance Variables in Ruby
How to Upload a Text File and Parse Contents into Database in Ror