I assume that you are using the date_select to create tags for the date. Another way you could do this is to use the form form helper for the day, month, year fields. Like this (for example, I used the date_at created field):
<%= f.select :month, (1..12).to_a, selected: @user.created_at.month %> <%= f.select :day, (1..31).to_a, selected: @user.created_at.day %> <%= f.select :year, ((Time.now.year - 20)..Time.now.year).to_a, selected: @user.created_at.year %>
And in the model you confirm the date:
attr_accessor :month, :day, :year validate :validate_created_at private def convert_created_at begin self.created_at = Date.civil(self.year.to_i, self.month.to_i, self.day.to_i) rescue ArgumentError false end end def validate_created_at errors.add("Created at date", "is invalid.") unless convert_created_at end
If you are looking for a plugin solution, I would check out the validates_timeliness plugin. It works like this (from the github page):
class Person < ActiveRecord::Base validates_date :date_of_birth, on_or_before: lambda { Date.current }
List of available verification methods:
validates_date - validate value as date validates_time - validate value as time only ie '12:20pm' validates_datetime - validate value as a full date and time validates - use the :timeliness key and set the type in the hash.
Jack Chu Feb 28 '09 at 9:14 2009-02-28 09:14
source share