Ruby 2.6.5 Rails 6.0.3.4 MySQL Visual Studio Code (GoogleChrome)
I'm creating something like a calendar app.
I want to save the date and start / end time separately in the DB, so I created a form using time_select
in the time type column for the time.
new.html.erb(Excerpt only for time form)
<div class="start-date-input">
<div class="form-title">
Start time
<span class="indispensable">Mandatory</span>
</div>
<div class='input-date-wrap'>
<%= f.time_select(:start_time, {class:'select-date', id:"start-time", prompt:'--', time_separator: ':'}, ignore_data: true) %>
</div>
</div>
<div class="start-date-input">
<div class="form-title">
ending time
<span class="indispensable">Mandatory</span>
</div>
<div class='input-date-wrap'>
<%= f.time_select(:start_time, {class:'select-date', id:"start-time", prompt:'--', time_separator: ':'}, ignore_data: true) %>
</div>
</div>
If you enter the date and time in the form as above and submit the data as a trial,
It seems that the date and time and each time are safely saved in the DB. Then when I try to display it in the view ...
show.html.erb
<div class="event-show-info-left">
<p><%= @event.day %></p>
<p><strong><%= @event.start_time %></strong></p>
<p><strong>↓</strong></p>
<p><strong><%= @event.finish_time %></strong></p>
</div>
!!!?!!!?
The mysterious year and month are also displayed for each time ... Who the hell is this 20th century child ...
Once, I checked the params of the transmitted data.
[1] pry(#<EventsController>)> params
=> <ActionController::Parameters {
(abridgement)
"day(1i)"=>"2020", "day(2i)"=>"12", "day(3i)"=>"2",
"start_time(1i)"=>"1", "start_time(2i)"=>"1", "start_time(3i)"=>"1", "start_time(4i)"=>"10", "start_time(5i)"=>"00",
"finish_time(1i)"=>"1", "finish_time(2i)"=>"1", "finish_time(3i)"=>"1", "finish_time(4i)"=>"13", "finish_time(5i)"=>"30",
(abridgement)
} permitted: false>
(1i)… year (2i)… Moon (3i)… Sun (4i)… time (5i)… minutes As you can see, the date is given along with each time.
Since it saves time, it is set to time type, so it will not be reflected in the DB,
As a specification of time_select
, it can be considered that it is handled with" 1 "added to the date.
This "1" is fetched into the view as it is, which seems to be the cause of the example 2000-01-01
.
** Then, how do you get only the time ...? ~ **
I would like to devise something on the view side. I checked ...
[Official] Ruby 2.7.0 Reference Manual instance method Time # strftime
strftime(format) -> String Returns the result of converting the time to a string according to the format string.
With reference to the above, strftime
seems to play an active role!
Use strftime
for the start time / end time class and format it into a string.
Since we only get the time, we use the following format.
--% H: 24-hour clock (00-23) --% M: Minutes (00-59)
I added .strftime ("% H:% M ")
to the view file.
show.html.erb(Fix)
<div class="event-show-info-left">
<p><%= @event.day %></p>
<p><strong><%= @event.start_time.strftime( "%H:%M" ) %></strong></p>
<p><strong>↓</strong></p>
<p><strong><%= @event.finish_time.strftime( "%H:%M" ) %></strong></p>
</div>
Only the time could be displayed !!
strftime
and time_select
were a great combination!
Regarding the handling of time, when it comes to large-scale implementation in the future I also felt that special consideration such as time zone and leap year would be required. I would like to take this opportunity to deepen my understanding of Time class classes.
This is a poor article for beginners, but I hope it will be of some help to you. Thank you for reading to the end.
11/11 21:40
Regarding the date and time format, from the aspect of DRY, I was instructed that it is better to correspond with the time zone and locale setting and the l
method.
Please refer to the article below.
Thank you @jnchito!
[Official] Ruby 2.7.0 Reference Manual instance method Time # strftime
[[Official] Ruby on Rails 6.0.3.4 Module ActionView :: Helpers :: DateHelper](https://api.rubyonrails.org/v6.0.3.4/classes/ActionView/Helpers/DateHelper.html#method-i- date_select)
[Qiita] Display the value of Time type data only in the time part