Working on a Rails CMS based project using BrowserCMS, there was a requirement to have a page which would display a calendar for the current month and onClick of each date, it has to make an Ajax request to query the database table and update a portion on the same page.
Everything is fine with the requirement, but a Rails calendar with each date sending an Ajax request to the controller?
My initial move was to look for some Rails plugin which would serve this purpose. Had a thorough Google search on this. Time was ticking, I managed to find one or two Rails calendar Plugin, but when tried on my project, it did not serve the exact purpose of each date sending the Ajax request. No much help was there on this in any Rails based forum as well.
This finally made me to think of writing the code for generating the calendar of my own. It was indeed going to be a tough task. But I had no other choice left in-front of me.
I have shared the code for the same through my GitHub account.
There in the GitHub repository you will find only a controllers folder and a views folder. Well that's all we need to get this done. If you have a Rails project up and running, you can go ahead and generate this Calendar Controller and use the code and modify it, style it as per your own requirement.
Here what I have done is, in the "calendar index" page you can see the calendar for the current month, with links for next and previous months, so that you can have a view of all the months. When you click on the next/previous links, the months partial is being updated. Similarly, onClick of each date will update a test "div" with the value of month and date. You can apply any logic here and make it work for your purpose.
Ruby/Rails Tips for the day:
=>How to get the starting day of the month ?
Loading development environment (Rails 2.3.5) >> date = Date.today => Fri, 29 Jan 2010 >> start_date = Date.parse "#{date.year}-#{date.month}-01" => Fri, 01 Jan 2010
=>How to get the last day of the month ?
>> last_date = (start_date >> 1)-1 => Sun, 31 Jan 2010
=>How to get the weekday of a particular date?
>> start_date.wday => 5
Special thanks to Nithin Bekal who helped me in getting this work done in no time.
If anyone uses this code for their application, please share with me the improvements made.