Rails - date input, allow only weekdays - ruby-on-rails

I have a form which has a field of type Date.
The following line creates an input for the field, where the user can choose the date.
<%= f.date_select :start_date %>
How can I make it display only weekdays as input options?

It's not possible to do what you want with a date_select helper. You have two alternatives (and one isn't really an alternative):
1) Write a validation in the model that fails if the date selected is not a weekend; or
2) Use a JavaScript date picker (like jQuery DatePicker), which is the only real solution. You can disable specific days or categories of days with DatePicker.

Related

Rails date_field with min and max but no default value

I would like a form date_field with min and max limits, but with no default value. Currently when I set min and max values, they are resulting in the yyyy of the date_field being constrained and set to a default value on page load.
Here is the rails view:
<%= f.date_field("start_date", min: #min_days, max: #today_date) %>
Here is the resulting html:
<input min="2022-06-19" max="2022-07-20" type="date" name="start_date" id="start_date">
Since the min and max are both within the same year, the date_field form loads with the year '2022' populated and not editable:
I am not able to find a way to have the date_field form load with all blank default values while still constraining the user to the provided min and max window when they use the date selector calendar widget:
The reason for this desired UI behavior is so the user can better understand that the date_field is a blank and optional form input.
Thanks
You cannot change this, it's nothing to do with Rails, it's part of the underlying HTML spec (and browser implementation).
Only way around it would be to use your own date widget.
FWIW, in my experience users aren't generally confused by this, as long as you have an obvious visual difference between optional and mandatory fields.

Date formatting in rails

I have a date in one of my models, which I am currently rendering in my simple form with
<%= f.input :issue_date, label:false %>
This, I believe automatically applies the date_select helper, causing it to render three very nice selection fields: one for day, one for month one for year.
The problem is that I really can't stand using select for the day and year (just seems more efficient to type it). Is there a good way to change the rendering of these fields? I really like how convenient this automatic rendering is; I'd just like it to render with one select box and two text boxes. Thanks for any suggestions.

Ordering by a custom date field?

I have a date field in my model which allows me to enter a date in the format of DD/MM/YYYY for example 10/01/2015 - How would I go about ordering by this model entry? So I have the soonest dates displayed first?
Any help/hints would be great :)

How do I let user pick date for time object?

I don't know why this is so difficult and I'm sure there's an easy answer, but i've been looking for about an hour and cannot find it. I have a form where I want someone to enter a time into a text field in the form hh:mm:ss . What I have now is:
<%= f.text_field :complete_time %>
:complete_time is an attribute of data type 'time'. So if someone enters '15:53:00', the :complete_time is stored as:
2000-01-01 15:53:00 UTC
I can't figure out how to let the user set the date of the time. Above the time field, I also have a datepicker field, but it is mapped to its own attribute. I would use the date picker in conjunction with the time text field if i knew how, but I don't. How do I let the user pick the date of the time object?
Well, there is this plugin you can use in with date picker to pick the time, it's really easy to use. http://trentrichardson.com/examples/timepicker

Create a date range dropdown with Ransack

I'm using Ransack for some simple searching of one of my models.
I would like to add a dropdown to the search with entries like 'Today', 'Yesterday', 'Current week', etc which would operate on the created_at field. Also it would be nice to have an entry in this dropdown to reveal 2 date boxes to pick a custom date range. How do I go about this?
One way that I can think of doing this is for the dropdown to populate the two (hidden) date boxes via JavaScript, I just don't know how I could easily reselect the proper entry in the dropdown when reloading the page.
use this
distance_of_time_in_words(obj.created_at,Time.now)+" ago"
This will return result like this:
about 2 hours ago

Resources