Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I'm guessing you're using the <code>date_select</code> helper to generate the tags for the date. Another way you could do it is to use select form helper for the day, month, year fields. Like this (example I used is the created_at date field):</p> <pre><code>&lt;%= f.select :month, (1..12).to_a, selected: @user.created_at.month %&gt; &lt;%= f.select :day, (1..31).to_a, selected: @user.created_at.day %&gt; &lt;%= f.select :year, ((Time.now.year - 20)..Time.now.year).to_a, selected: @user.created_at.year %&gt; </code></pre> <p>And in the model, you validate the date:</p> <pre><code>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 </code></pre> <p>If you're looking for a plugin solution, I'd checkout the <a href="http://github.com/adzap/validates_timeliness/" rel="noreferrer">validates_timeliness</a> plugin. It works like this (from the github page):</p> <pre><code>class Person &lt; ActiveRecord::Base validates_date :date_of_birth, on_or_before: lambda { Date.current } # or validates :date_of_birth, timeliness: { on_or_before: lambda { Date.current }, type: :date } end </code></pre> <p>The list of validation methods available are as follows:</p> <pre><code>validates_date - validate value as date validates_time - validate value as time only i.e. '12:20pm' validates_datetime - validate value as a full date and time validates - use the :timeliness key and set the type in the hash. </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload