Rails의 time_select에서 기본적으로 입력되는 날짜를 화면에 있는 다른 필드의 입력 값으로 설정
11787 단어 form_helperTime_SelectdatetimeRails
배경으로 한 일
Rails의 앱으로 이런 느낌의 양식을 만들었습니다.
실시일을
date_field
로 선택하고 시작 시간과 종료 시간을 각각 time_select
로 선택합니다.실행 환경은
rails 5.2.4.2
입니다.
view 파일의 내용은 이런 느낌입니다. 데이터 형식은 모든 필드에서
DateTime
형식이었습니다.보기
.form-group
= f.label :date, "実施日"
= f.date_field :date
.form-group
= f.label :begin_at, "開始時間"
= f.time_select :begin_at
.form-group
= f.label :closed_at, "終了時間"
= f.time_select :closed_at
발생한 문제
이 양식에서 데이터를 제출하면 다음과 같습니다.
#<Activity:0x00007fc97030fe30
id: 1,
date: Fri, 11 Sep 2020 00:00:00 JST +09:00,
begin_at: Mon, 1 Jan 0001 00:10:00 JST +09:00,
closed_at: Mon, 1 Jan 0001 00:20:00 JST +09:00
time_took: 600,
created_at: Fri, 04 Sep 2020 13:53:01 JST +09:00,
updated_at: Fri, 04 Sep 2020 13:53:01 JST +09:00>
…네? ?

어째서, 서기 1년의 1월 1일의 시간이 되고 있습니까.....
여러가지 조사한 바, 이것은 Rails의 사양일 것 같지 않습니다.
여기 기사처럼 시간만 입력하는 방법도 있었습니다.
(단,
begin_at
, closed_at
의 데이터형은 Time형으로 할 필요가 있을 것 같습니다)time_select에서 시간만 입력하도록 허용 [rails]
다만, 이번은,
time_took
라고 하는 다른 컬럼에서 「걸린 시간」을 계산하고 있거나, 향후 이러한 숫자를 분석에 사용할 것으로 예상되고 있다고,begin_at
, closed_at
의 값을 사용해 돌리기 때문에, 정확하게 일시를 나타내도록(,) 했습니다.했던 일
모델에 다음과 같은 메소트를 정의하고
time_select
콜백으로 호출합니다.모델
class Activity < ApplicationRecord
before_save :set_the_day_implement
private
def set_the_day_implement
year = self.date.year
month = self.date.month
day = self.date.day
self.begin_at = self.begin_at.change(year: year, month: month, day: day)
self.closed_at = self.closed_at.change(year: year, month: month, day: day)
end
end
코드 해설입니다.
date
형식으로 양식에서 입력한 '실행일(before_save
)'의 해를 각각 검색할 수 있습니다. month, day도 각각 같습니다.# dateフィールドの値が '2020-09-04'の場合
self.date.year
#=> 2020
self.date.month
#=> 9
self.date.day
#=> 4
또한,
self.date.year
, date
의 값에는, 각각 다음과 같은 형태로 액세스 할 수 있습니다.self.begin_at
#=> Mon, 1 Jan 0001 00:10:00 JST +09:00
self.begin_at.year
#=> 1
self.begin_at.month
#=> 1
self.begin_at.day
#=> 1
self.begin_at.change(year: 2020, month: 9, day: 4)
#=> Fri, 4 Sep 2020 00:10:00 JST +09:00
그러나 마지막으로
self.begin_at
에 self.closed_at
에 정의 된 내용을 할당하지 않으면 열 값이 업데이트되지 않습니다. (← 여기를 알아차리지 않아 상당히 빠졌다)# dateフィールドの値が '2020-09-04'の場合
year = self.date.year
month = self.date.month
day = self.date.day
self.begin_at.change(year: year, month: month, day: day)
#=> Fri, 4 Sep 2020 00:10:00 JST +09:00
self.begin_at
#=> Mon, 1 Jan 0001 00:10:00 JST +09:00
# 西暦1年に戻っている
self.begin_at = self.begin_at.change(year: year, month: month, day: day)
self.begin_at
#=> Fri, 4 Sep 2020 00:10:00 JST +09:00
# 代入するとカラムの値が更新される
글쎄, 이제 무사히 원하는 데이터를 얻을 수있었습니다.
결국 뷰에 표시되는 것은 이런 데이터입니다. (괄호 안의 숫자는 다른 콜백으로 만든 "걸린 시간"입니다.)

향후 활용이 예상되는 데이터이므로, 올바른 일자의 데이터가 제대로 들어가 만족했습니다

계속해서 일 노력해 갑니다♪
Reference
이 문제에 관하여(Rails의 time_select에서 기본적으로 입력되는 날짜를 화면에 있는 다른 필드의 입력 값으로 설정), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/tanutanu/items/8e41c28c52e045bb0451텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)