Using accepts_nested_attributes_for in rails with datetime
-
04-06-2021 - |
Вопрос
I'm trying to use a repeatable section of datetimes in a rails app using accepts_nested_attributes_for.
I have the following:
accepts_nested_attributes_for :meetings, :allow_destroy => true, :reject_if => lambda { |a| a[:starting_at].blank? }
In my view I am currently using a rails date_select
and a time_select
.
It won't save the submission though and I think it is because of starting_at
being a multi part datetime attribute. I have used a[:starting_at].blank?
in the past when working with text_fields but it doesn't seem to work with date_select/time_select fields.
Can anyone shed any light?
My other option would be to use a text_field
but I'm not sure how to do this so that the date and time are easy (and user-friendly to select) because if I use f.text_field :starting_at
it would combine both date and time in a single text field.
Решение 2
In the end it was easiest to just go with a text_field
. I just need to find a date time picker now (possibly the jquery UI one).
Другие советы
You could easily use a text_field, and prob the best way to go.
You keep in your view as you have
f.text_field :starting_at
In your model:
def starting_at=(input)
# use a shared converter to convert the format from your textfield
x = self.string_to_date(input, I18n.t('date.formats.default'))
write_attribute(:starting_at, x)
end
def starting_at
# Same but in reverse
return date_to_string(read_attribute(:starting_at), I18n.t('date.formats.default') )
end