Designer
Designer

Reputation: 1111

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

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

Upvotes: 3

Views: 5858

Answers (3)

lafeber
lafeber

Reputation: 2780

This has to do with the browser support for date_field. It has improved quite a lot recently, see https://caniuse.com/mdn-html_elements_input_type_date

Upvotes: 1

Dheeraj Maheshwari
Dheeraj Maheshwari

Reputation: 407

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 %>

Upvotes: 3

kykyi
kykyi

Reputation: 385

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

Upvotes: 1

Related Questions