# Interesting problem

**URL:** <https://www.ruby-forum.com/t/interesting-problem/132765>\
**Category:** Rails\
**Created:** [March 24, 2008, 3:40pm UTC](https://www.ruby-forum.com/t/interesting-problem/132765 "2008-03-24T15:40:11Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![sjglazer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ee59a6/32.png) [@sjglazer](https://www.ruby-forum.com/u/sjglazer)\
**Post date:** [March 24, 2008, 3:40pm UTC](https://www.ruby-forum.com/t/interesting-problem/132765/1 "2008-03-24T15:40:11Z")

</div>

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:

 \<%= f.time\_select :mon\_open, :minute\_step =\> 15, :twelve\_hour =\> true, :default =\> { :hour =\> 13, :minute =\> 0 } %\> \<%= f.time\_select :mon\_close, :minute\_step =\> 15, :twelve\_hour =\> true, :default =\> { :hour =\> 23, :minute =\> 0 } %\> 

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
