Topic: date_select helper and default values

... and i'm back with another question.

I am using the date_select helper in my form and I want to set the date when the page loads to something specific. Is there a way to do this? If not, is there a way to do this other than by manually adding the fields that I want one by one?


Re: date_select helper and default values

The date_select method is for editing a model's date attribute. If you want to set the initial value you would set the date for the model. For example:

def new
  @my_model = => # use any date you want

If this isn't a model's attribute, use the select_date helper instead, this allows you to insert a date directly when calling the method.

Railscasts - Free Ruby on Rails Screencasts

Re: date_select helper and default values

Okay I guess I am beginning to see a theme here. I didn't know that the Model was so closely tied to forms. It makes sense since Rails seems to do a lot of magic to make things "just work"  whenever a new form is created.

I am wondering about using things like select_date and select_time. Right now, I make form elements that are part of the model attribute (ie. date_select), and whenever I want to allow a user to edit the values, the fields are automatically populated with the correct values. I assume this part of the reason to use things like date_select. If I use something like select_date, would i need to do something special to populate the fields with the correct values from the database?

Second question (along the same lines). I have two fields: start_time and end_time in my form. First off, those times are in the following format: 6:00 AM, 6:30 AM, 7:00 AM etc. I generated an array with a set of values that I wanted and passed them into a The data type for the start_time and end_time fields in the database is "Time". I am of course having trouble populating the start_time and end_time in the form with the correct values when editing. I do think it has something to do with the "Time" in the database not matching up with the time format I have generated for my select form element. How might I fix this?


Re: date_select helper and default values

Well, I found a solution to my second problem. I'll post it here for archival purposes. I am storing two things in my database:

start_time with data type Time
end_time with data type Time

I wanted to use 2 select boxes: one for start_time and one for end_time. The time values were in a specific format within a specific range. The times were in the format '%I:%M %p' from 6:00am - 8:00pm. So I created a private method within my controller that generated an array that looked like this:

[6:00 AM, Time.parse('2000-01-01 6:00:00 AM')],
[6:30 AM, Time.parse('2000-01-01 6:30:00 AM')],
[7:00 AM, Time.parse('2000-01-01 7:00:00 AM')]

So the display value in the select box would be '6:00 AM' and the select value is something like 'Sat Jan 01 6:30:00 -0700 2000'. The thing that caught me was that the Time data type in MySQL doesn't return just a time in the format '%I:%M %p'... it instead returns a default date as well as the time. The only way to make something populate the time select box properly was to tack on the date as well to the value.

If anyone has a better way to do this, I would love to hear it.

Last edited by Ceres (2007-01-21 20:30:03)

Re: date_select helper and default values

Oh, I also found a handy plugin called FlexTimes that may help someone else attack this problem in another way. Didn't help me out much, but it might help someone else out: … mes+Plugin