views:

260

answers:

3

I may have over-complicated what i need to do but this is what i now have.

I jab a jobs controller that have 2 fields

starts_at as DATETIME
end_time as DATETIME

I save the event_date in a form using a

calendar_date_select :starts_at ,:time => false

I save the date time as

time_select :starts_at, {:twelve_hour => true}

This save the Event date i.e 12/26/2009 and the start time as 7:00 pm

I what need to to also save the event end_time without having to re-enter the date, just the time

time_select :end_time, {:twelve_hour => true}

If i just use the abouve the time is correct but the date dafaults the 01/01/2000

How do I save the date with the same date from starts_at?

A: 

In your controller, if the :end_time is nil, set it to the :starts_at variable like so:

object.end_time = object.starts_at unless not object.end_time.nil?

You may be able to shorten the end to

object.end_time = object.starts_at unless object.end_time.present?

but I'm not sure which version of Rails you are using and what the default implementation of .present? does for dates.

Topher Fangio
Where in my controller should this be. I have placeed it above my update acction and nothing happens. but also wouldnt this set just the date forgeting the time?
Fresh
You would need to do this in both your new and edit controller methods if you want them to be editable by the user. If you don't want the user to have any control over it, then you could put it in the create and update methods at the top like you said you have done.
Topher Fangio
A: 

You could do this in the model or controller, but isn't it possible that an event will start late at night and end on the following day? To allow for this possibility I'd recommend having two calendar_date_selects and auto-assigning the ending date when the starting date is chosen (so the second calendar_date_select will rarely be used).

If you really don't want two calendars, maybe use a hidden field for the ending date that follows the start date.

I wouldn't enforce this same-date rule below the interface level unless you're absolutely sure different start and end dates will never occur.

Alex Reisner
A: 

I may be asking my question wrong.

What I need to do is update the same field twice

first the date using a date_select and then the same field using a time_select.

using the same form.

Fresh