views:

242

answers:

3

I have two fields, 'from_date' and 'to_date' on my rails form. When a user enters the dates I have to display the number of working days i.e, exclude (sat, sun and a list of other official holidays in a database table).

Is this something that should be done in the client side javascript Or is this something that should be done by making an ajax request to rails.

Can you tell me how I can accomplish this.

thanks much.

+1  A: 

Depending on how often that feature would be used I'd opt for doing both, create a .js-file (or generate dynamically and just make sure rails caches it) and try it with javascript locally for instant gratification. Then make sure it's correct on the server side on saving.

The upside with doing it both on the client and on the server is that you may save some hassle when the guy can see it directly on the site without doing a request. But if they have javascript disabled it'll still work. And if it's anything important you should never trust what you get from a web form. :)

ba
I need to display the number of working days to the user before they submit the form. If I am using client side js, how do I get the list of holidays from the database. Are you suggesting making ajax request with observe_field prototype helper. Do I do the calculation in ruby or js before I display to the user. Some sample code will be helpful.
I suggest that you in your invalid dates controllers index method return all invalid dates as JSON or similar.On the page/controller you need to calculate working days make sure you include that JSON-file and bind it to an array. Then loop through all dates in your range and match them against that array and the days that are weekends.Advantage is that you can cache the invalid dates but still have them in the database, easy to change. But you also don't have to query the database/rails every time someone needs to calculate the days, as long as they have javascript enabled. :)
ba
Thanks. Still unclear on a few points. 1. When do I call the 'invalid_dates' controller 'index' action to return the index.json view. Do I do this on the 'blur' event of the input date fields or on document.onload2. 'Loop through the dates in your range ...' is this javascript code or ruby code you are referring to?thanks.
are you saying I make the call in a script tag like <script type="text/javascript" src="http://domain.com/invalid_dates/index?jsonp=parseResponse"></script>When do I do the looping or the calculation. On the 'blur' or 'change' event of both dates do I check if they are not null and loop through the array in javascript?
Add the include on the initial page load on those pages that will need it, will speed things up since it'll be there when it's needed.On blur when both are filled I think is good. Or on submit, you know your business requirements better than I do :)
ba
thanks for the suggestions.
A: 

This patch might give you some ideas. https://rails.lighthouseapp.com/projects/8994/tickets/2304-add-concept-of-weekdays

Tim Snowhite
this was very useful. thanks!
A: 

You might be interested in business_time.

Ben Marini