Format text_field helper?

Is it possible to apply strftime formatting to the value of a text input field using the Rails text_field helper? In the form that I use to create or edit an entry, I have a text input field that is populated with the calendardates option Select javascript. I click on the text box and a small calendar appears. After I select the year, month, day and time, the calendar leaves, and the rest in the text input is a text representation of the date. This is what I see if I go to edit a post.

I would like to change how this date is displayed in the text input field. In other templates where this date is displayed, I can use strftime, but I do not know how (or even if it is possible) to format the value of the input text field.

Thanks.

Steve

+3
source share
2 answers

Option 1: If you have a default time format for your application, it text_fieldwill use it. For example, you might have config/initializers/time_formats.rbone containing the following:

Time::DATE_FORMATS[:default] = '%m/%d/%Y %I:%M %p'

Option 2: You can override text_field valueusing strftimedirectly. For example:

<%- scheduled_at_string = form.object.scheduled_at && form.object.scheduled_at.strftime("%m/%d/%Y") -%>
<%= form.text_field :scheduled_at, :value => scheduled_at_string %>

(You might want to clean it with an assistant.)

+6
source

Got this, but should have used a regular HTML input element, not a Rails helper:

<input type="text" id="assignment_starts_at" name="assignment[starts_at]" class="jqueryCDS" size="30" value="<%= @assignment.starts_at.strftime('%A, %b %e, %G - %I:%M %p') if @assignment.starts_at %>" />
0
source

Source: https://habr.com/ru/post/1725349/


All Articles