What is the most native way of adding a date picker to a Ruby on Rails 6 project?

Viewed 1361

I'm building a simple project where date input plays an important role. I don't want to add an another gem and heavy js library like jquery, bootstrap.

What is the most lightweight solution where I can let users to select/enter date? The pure Ruby way.

Thank you


EDIT:

I use date_field type, but it lets me enter any value. It does not format my input as date. Code sample:

<%= form.date_field :date %>

enter image description here


SOLUTION (temporary):

Right! I needed to use date_select, not date_field : )

<%= form.date_select :date %>

enter image description here

3 Answers

Rails has a whole heap of in built form helpers for selecting dates and other data types: https://api.rubyonrails.org/v6.0.0/classes/ActionView/Helpers/DateHelper.html#method-i-select_date

I am not sure what you mean by 'pure ruby' as a date picker is html/css with some optional js, but the select_date form helper (and all other rails form helpers) are ruby objects under the hood, which, given arguments, will return HTML.

In addition to the above link, you should check out these docs for how to implement inside a form: https://guides.rubyonrails.org/form_helpers.html#dealing-with-basic-forms

Your date field should also work but you can try this also , may be this work for you.

<%= form.date_field :date, min: 0.days.ago %>
Related