How to prevent past dates from being entered in Rails forms

Introduction

I want to prevent past dates from being selected when I have a date entered from the calendar. Date type is used for the column of the model to be saved. Use form_with to create a form in the view.

Development environment

ruby 2.6.5 Rails 6.0.3.3

Method

ruby:index.html.erb



<%= form_with model: @order, url: item_order_path, local: true do |f|%>
   <%= f.date_field :start_date, class:"order-date-form %>
<% end %>

You can display the calendar and select the date.

However, at present, you can also select past dates.

Add a description to solve this.

ruby:index.html.erb



<%= form_with model: @order, url: item_order_path, local: true do |f|%>
   <%= f.date_field :start_date, class:"order-date-form, min: Date.current %>
<% end %>

The calendar is displayed in the same way, but past dates can no longer be selected.

"Date" is provided by Active_support and seems to be available by default. "Date.current" is recommended instead of "Date.today".

Click here for details Rails Guide

finally

Try adding each to prevent with validation or controller.

Recommended Posts

How to prevent past dates from being entered in Rails forms
How to prevent direct URL typing in Rails
[Rails] How to write in Japanese
[Rails] How to prevent screen transition
How to introduce jQuery in Rails 6
How to install Swiper in Rails
[Explanation in 3 lines] How to prevent programming school technical articles from being displayed in search results
How to implement search functionality in Rails
How to change app name in rails
How to use custom helpers in rails
[Rails] How to convert from erb to haml
How to insert a video in Rails
How to use MySQL in Rails tutorial
[rails] How to configure routing in resources
How to implement ranking functionality in Rails
How to use credentials.yml.enc introduced in Rails 5.2
How to display the text entered in text_area in Rails with line breaks
How to link Rails6 Vue (from environment construction)
[Rails] How to use select boxes in Ransack
How to translate Rails into Japanese in general
How to get Class from Element in Java
How to separate .scss by controller in Rails
How to conditionally add html.erb class in Rails
How to implement a like feature in Rails
How to easily create a pull-down in Rails
How to use JQuery in js.erb of Rails6
[Ruby on Rails] How to install Bootstrap in Rails
How to make a follow function in Rails
[Rails] How to use PostgreSQL in Vagrant environment
How to check Rails commands in the terminal
How to set the display time to Japan time in Rails
How to implement guest login in 5 minutes in rails portfolio
How to implement a like feature in Ajax in Rails
[Ruby on Rails] How to write enum in Japanese
How to write Rails
[Ruby On Rails] How to reset DB in Heroku
[How to insert a video in haml with Rails]
How to uninstall Rails
How to write a date comparison search in Rails
How to download images from AWS S3 (rails, carrierwave)
How to query Array in jsonb with Rails + postgres
How to prevent editTextPreference of android PreferenceFragmentCompat from breaking
[Rails 6] How to set a background image in Rails [CSS]
[Rails] How to load JavaScript in a specific view
[Rails] How to display an image in the view
How to write a migration from Rails datetime type to date type
[Rails] How to define macros in Rspec and standardize processing
Prevent operations! How to securely update Rails manually using transactions
How to store a string from ArrayList to String in Java (Personal)
How to deploy jQuery in your Rails app using Webpacker
Prevent unused imports from being deleted when saving in Eclipse
How to display a graph in Ruby on Rails (LazyHighChart)
Super easy in 2 steps! How to install devise! !! (rails 5 version)
[Rails 5] How to use gem gon ~ How to pass variables from Rails to JS ~
[Rails5] japanMap link How to write parameters in js.erb file
[Docker context] ~ How to access docker in remote environment from VScode ~
Prevent unused imports from being deleted when saving in Eclipse
[Rails] How to display information stored in the database in view
[Rails] How to write user_id (foreign key) in strong parameter
[rails] How to post images
[Rails] How to use enum