views:

680

answers:

1

I have a form that has been split out into a partial so I can use it both in the new view and the edit view. I have several dropdowns(selects) that are populated from a static array generated in a model.

In location model:

def open_close_times
@times = '','12:00 AM', '12:30 AM', '1:00 AM', '1:30 AM', '2:00 AM', '2:30 AM', '3:00 AM', '3:30 AM', 
         '4:00 AM', '4:30 AM', '5:00 AM', '5:30 AM', '6:00 AM', '6:30 AM', '7:00 AM', '7:30 AM', 
         '8:00 AM', '8:30 AM', '9:00 AM', '9:30 AM', '10:00 AM', '10:30 AM', '11:00 AM', '11:30 AM', 
         '12:00 PM', '12:30 PM', '1:00 PM', '1:30 PM', '2:00 PM', '2:30 PM', '3:00 PM', '3:30 PM',
         '4:00 PM', '4:30 PM', '5:00 PM', '5:30 PM', '6:00 PM', '6:30 PM', '7:00 PM', '7:30 PM',
         '8:00 PM', '8:30 PM', '9:00 PM', '9:30 PM', '10:00 PM', '10:30 PM', '11:00 PM', '11:30 PM'

end

So the selects in my form look like this:

<label for="hours_sunday_open">Open:</label><%= f.select(:hours_sunday_open, @location.open_close_times) %>

and I call the partial like so:

<%= render :partial => "form", :locals =>{ :f => f} %>

how do I get the selects to mark the "selected" info returned from the edit controller?

A: 

Rails will automatically mark as "selected" the item that matches the item returned from the controller.

So if your edit controller returns "1:00", that will be selected if it's present in your select list.

yalestar
Doesn't seem to be the case for me... all selects are blank... If I change them to text_fields then I see the correct info?
Nick Faraday
Found the issue... I was saving the data as time field in the DB, but with the AM, PM in the select array that doesn't work. Changing the DB to store the times as a VARCHAR worked now all selects are auto filled.
Nick Faraday