Sunday, October 30, 2011

Using jQuery with Rails 3.0

I've been working on a little Rails app that is based on Michael Hartl's Ruby on Rails Tutorial. I'm going back now (after some customization) and adding new functionality to the code. One of the exercises  in the book was to add a simple JavaScript counter to the micropost textbox.

I decided to go with jQuery and found the implementation to be quite simple. First you need to install jQuery in Rails, and you can find instructions on how to do this here.


Once jQuery was installed, I simply had to add the jQuery code to my form, as well as modify the original form code to limit the characters to 140 with f.text_area :content, :maxlength => 140 :




Just for reference, the original code for the form (without the textbox countdown) was as follows:



Feel free to use or modify this code if you found it helpful to you!

1 comment:

  1. Wow, thanks for the excellent post! I needed this functionality and with your post I was able to get it working within 5 minutes! Thank you so much and keep up the great sharing! Cheers!

    ReplyDelete