Hi there everybody,
I am a few weeks into my first ROR project and have across a problem I
was hoping someone could offer some help with. I have a database that
stores restaurants and I am keeping the delivery hours for each day in
the table.
Right now I have the following time fields in my restaurant table for
the information:
mon_open, mon_close, tue_open, tue_close……sun_open,sun_close
Right now in my restaurant view I have 14 time_select controls like the
following to get the information when a new restaurant is created:
Open:
<%= f.time_select :mon_open, :minute_step => 15,
:twelve_hour => true, :default => { :hour => 13, :minute => 0 } %>
<label for="restaurant_mon_close">Close:</label>
<%= f.time_select :tue_close, :minute_step => 15,
:twelve_hour => true, :default => { :hour => 23, :minute => 0 } %>
Now so far everything works fine and there are no problems. However I
don’t like having 14 time drop downs on the page. What I would like to
do is have two time_selects(one for the opening hour and one for the
closing hour) with seven check boxes next to it(one for each day of the
week). That way if the delivery hours are the same everyday the user can
just select all the check boxes. If there are different hours for lets
say the weekend. I will have a “add hours†link they can click that will
use ajax to add another two time_select drop downs along with the check
boxes.
Now I have no problem with the ajax part but I cannot figure out the
best way to communicate the information back to the controller. I
thought maybe something along the lines of having 14 hidden time_selects
mapped directly to the restaurant attributes and then when someone
selects a checkbox I could use JavaScript to fill in the value.
Something along the lines of:
Hidden:
Then:
Open:
<%= time_select “Day1”, “Day1”, :minute_step =>
15, :twelve_hour => true, :default => { :hour => 13, :minute => 0 } %>
<%= check_box_tag “Mon”, “Mon”, false, {:onchange =>
“daySelected(‘Mon’,‘Day1’ )”} %>Mon
<%= check_box_tag “Tue”, “Tue”, false, {:onchange =>
“daySelected(‘Tue’,‘Day1’ )”} %>Tue
<%= check_box_tag “Wed”, “Wed”, false, {:onchange =>
“daySelected(‘Wed’,‘Day1’ )”} %>Wed
<%= check_box_tag “Thu”, “Thu”, false, {:onchange =>
“daySelected(‘Thu’,‘Day1’ )”} %>Thu
<%= check_box_tag “Fri”, “Fri”, false, {:onchange =>
“daySelected(‘Fri’,‘Day1’ )”} %>Fri
<%= check_box_tag “Sat”, “Sat”, false, {:onchange =>
“daySelected(‘Sat’,‘Day1’ )”} %>Sat
<%= check_box_tag “Sun”, “Sun”, false, {:onchange =>
“daySelected(‘Sun’,‘Day1’ )”} %>Sun
<%= link_to_function "Add Hours" do |page|
page.insert_html :bottom, :hours, :partial
=>‘hours’
end %>
</tr>
And some javascript:
function daySelected(whichButton,whichTime)
{
if(whichButton == ‘Mon’)
// set the hidden control for mon with the value from whichTime
}
I played around for quite a while and just cannot get it to work. The
first problem I am having is trying to get the correct time value
associated with the selected day. The id’s of the time_selects are very
strange. In any case would like to know if there is a better/easier
approach to what I am trying to do.
Thanks,
steve