El nino
El nino

Reputation: 239

Date Issue with Ruby on Rails

I just working on a simple Ruby on Rails application that keeps the data information of staff, but I realized that I needed a way to check that the date of employment will definitely need to be the beginning of the date of resignation.

<div class="field">
   <%= f.label :date_of_employment %><br />
   <%= f.date_select :date_of_employment, :start_year =>1990 %>
</div>
<div class="field">
   <%= f.label :date_of_resignation %><br />
   <%= date_select :date_of_resignation, :start_year => Time.now %>

Upvotes: 1

Views: 139

Answers (2)

Ethan
Ethan

Reputation: 1616

One of the simple solution you can consider is to have a javascript function to verify these two date on form submit. This way your server does not need to process invalid data.

We can perform validation at Rails model level before saving to database. For example these lines of code in the model will validate the data before saving

validate :verify_date
def verify_date
   errors.add("Invalid date") if date_of_resignation < date_of_employment
end

Upvotes: 0

nmott
nmott

Reputation: 9604

You should check validates timeliness gem, if you are happy to add outside code to your project. It adds all kinds of time/date based validations.

Including validating that one field is before/after another. For instance, the following commands could be used in you model:

validates_datetime :date_of_resignation, :after => :date_of_employment

This gem doesn't rely on javascript to work, it's pure Ruby.

Upvotes: 2

Related Questions