0

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.

user1116573
  • 2,817
  • 4
  • 17
  • 27

2 Answers2

0

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
Andrew Cetinic
  • 2,805
  • 29
  • 44
  • Hi Andrew, does rails not take care of this anyway if I use a text_field in the view for the datetime where the value is in a rails standard format? When I said "I'm not sure how to do this", I actually meant to say that I want to make the date easy to select (using a datepicker, since it won't offer simple dropdowns) and the time easy to select somehow also (I'll update the Question). – user1116573 May 02 '12 at 18:30
0

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).

user1116573
  • 2,817
  • 4
  • 17
  • 27