1

In my form I have two date fields, :start_date and :end_date, related to a job. There is a month and a year.

Just like on LinkedIn, the end year might not be relevant, for your current job.

Therefore, I'm looking for the way to replace the :end_date with "present" when the User tick the box "current position".

What's the right way to do this?

Update

My code at the moment: <%= f.date_select :end_date, { discard_day: true } %>

DoeJ
  • 25
  • 3
  • @Udaykumardas Wouldn't that just set the present date for good? The thing is there is no end date in that case, since the job has not ended. I updated with my code, which is fairly basic. – DoeJ Nov 02 '16 at 07:33

1 Answers1

1

I wouldn't set a value that discard's end date, I'd set present_job to true from the checkbox (add this attribute to the jobs table). This would ignore or clear the end date.

before_validation :clear_end_date, :if => :present_job

validate :end_date, :presence => true, :unless => present_job

def clear_end_date
  self.end_date = nil
end

Also, use this checkbox to visually disable and clear the end_date in the form. You just need to add an event to see if the state changes and update the end date field.

$(".checkbox").change(function() {
    if(this.checked) {
        // Disable end_date select box and make other visual changes
    }
});

jQuery checkbox checked state changed event

Community
  • 1
  • 1
Mark Swardstrom
  • 17,217
  • 6
  • 62
  • 70