All threads / Flatpickr with "Present" value as well.

Ask A Question

Notifications

You’re not receiving notifications from this thread.

Flatpickr with "Present" value as well.

Gerard Donnelly asked in General

I am currently adding date ranges for jobs in my app. I want the user to pick the start and end dates using flatpickr gem. I also want the user to be able to set the field to "Present" if they are still working there. I was wondering what the best way to implement this would be.
See the screenshots from how LinkedIn do it.

[https://imgur.com/a/Jj1zoTL]

Thanks for your help guys.

Hey Gerard,

I would probably use Javascript to do two things when checked:

  1. Hide/show the end date field
  2. Clear the value for the date fields

Flatpickr write the selected value to a form field, so it can be submitted to the server. That's the field you'll want to clear out in the JS.

Then just make sure your end date field is marked as optional in your database so it can be null.

Thanks for your explanation Chris.
To confirm.

Have 3 fields:
Start Date - Date Field
End Date - Date Field
Currently Working - Boolean

If Currenly Working (Boolean) is true; hide End Date field and clear any value from it.

Thanks,

Gerard

Yep, and I don't know that you necessarily need the currently working boolean in the database. You can just add some methods in your model to delegate to the end_date column.

class Job
  def currently_working?
      !end_date?
    end
end

Then you can just use that method to set the checked attribute on the checkbox by default when editing.

Thats a great help Chris.

Thanks.

Join the discussion

Want to stay up-to-date with Ruby on Rails?

Join 33,399+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    logo Created with Sketch.

    Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

    © 2020 GoRails, LLC. All rights reserved.