I am working learning RoR. I have one requirement to input dates from user but it should always be current date time or future. In the input, user is entering start-datetime and end-datetime. Currently I using below syntax and helper class to select date and time.
<div class="field">
<%= form.label :start_time %>
<%= form.datetime_select :start_time %>
</div>
As per current form, this is what user can select from form:
Here, I wanted to select date and time range in such a way that it is always from current date time, but not the old date time.
Here, I want to capture the future event information from user and store into database.
Below is the Event model:
class Event < ApplicationRecord
validates :start_time, presence: true
validates :end_time, presence: true
validate :valid_start_and_end_time
def start_time
super || end_time
end
private
def valid_start_and_end_time
start_time <= end_time
end
end
and EventController:
class EventsController < ApplicationController
before_action :set_event only: [:show, :edit, :update, :destroy]
def index
@event = Event.all.where('start_time > ?', Time.now).sort_by(&:start_time).sort_by(&:title)
end
def create
@event = Event.new(event_params)
@event.save
end
private
def event_params
params.require(:event).permit(:title, :start_time, :end_time)
end
end
Basically, what I want to do here is:
1. We want user to enter start date time and end date time, all should be future events or atleast add the validation.
2. In model, we want to sort the events index by start_time and then by title.
Can someone please help me to understand how it can be done in best approach so that I can enhance my learning in RoR.
Any help would be really appreciated here.