29 January 2010

Generating Calendar With Rails

Download the source code from GitHub

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.

10 comments:

  1. Since you mentioned this problem to me, I was looking around for a good calendar plugin that performs a similar function and couldn't find anything. Maybe you could package the code into a plugin?

    ReplyDelete
  2. The ROR duo at it again??? Cool man.. Do try and make a plugin outta this.. good luck for that.

    ReplyDelete
  3. Well guys, Thanks for the comment. Yea, creating a plugin is a great idea. Will see if I get time to dedicate for this purpose

    ReplyDelete
  4. Have you ever thought about writing an ebook or guest authoring on
    other sites? I have a blog centered on the same ideas
    you discuss and would love to have you share some stories/information.
    I know my viewers would value your work. If you are
    even remotely interested, feel free to shoot me an e mail.



    Here is my website; ixwebhosting Ratings
    My website :: ixwebhosting Evaluations

    ReplyDelete
  5. My brother suggested I might like this web site. He was entirely right.
    This post truly made my day. You can not imagine just how much time I had spent for this info!
    Thanks!

    My web site - Eleven2 Testimonials

    ReplyDelete
  6. I really like reading through a post that can make
    people think. Also, thanks for permitting me to comment!


    Also visit my weblog vexxhost reviews

    ReplyDelete
  7. First off I want to say terrific blog! I had a quick question that I'd like to ask if you don't mind.
    I was interested to find out how you center yourself and clear your mind before writing.
    I've had a difficult time clearing my mind in getting my ideas out. I truly do enjoy writing however it just seems like the first 10 to 15 minutes are generally lost just trying to figure out how to begin. Any ideas or hints? Appreciate it!

    my web-site ... web hosting streaming media

    ReplyDelete
  8. I believe that is one of the most vital information for me.
    And i am satisfied reading your article. But should remark on few common things,
    The web site taste is perfect, the articles is in reality nice : D.
    Excellent job, cheers

    My web page: weebly.com

    ReplyDelete
  9. Excellent pieces. Keep writing such kind of information
    on your site. Im really impressed by your site.

    Hi there, You've done an incredible job. I'll certainly
    digg it and in my opinion suggest to my friends. I'm sure they'll
    be benefited from this site.

    My blog post :: webhostingtop3.com

    ReplyDelete
  10. Hola! I've been reading your web site for some time now and finally got the bravery to go ahead and give you a shout out from Porter Texas! Just wanted to say keep up the good job!

    My web site: install horde webmail shared hosting

    ReplyDelete

Share your thoughts, Lets have a discussion :)