07 Sep 2009

Ruby on Rails Caching And JavaScript Techniques

ajax, javascript, rails posted by sid

Cross posted from darthsid

While implementing caching in a recent rails project I came across some typical caching issues. In a lot of pages the content is same for all users but certain components in them have user specific actions. As an example, I have a page listing all public messages that users have posted(similar to the public timeline in twitter) but actions on those messages are user specific(eg: only owner or admin can delete a message). Also, most of these actions use ajax and the rails authenticity token in them also gets cached resulting in subsequent failures if the session changes. Another issue was that the timestamps in most pages is fuzzy and they become irrelevant if a page gets cached for too long. I could have created separate caches for each user but if the user base really grows managing the caches would become a nightmare and that would still not solve the authenticity token and the timestamp problem. The simplest solution was to use JavaScript, more specifically jQuery.
Read more

08 Aug 2008

Configuring TinyMCE SpellChecker with rails application

javascript, rails posted by Akhil Bansal

Last month I spent much time while configuring tinymce’s spellchecker with my rails application. But finally I got it working and thought I could be a good post. To configure this I took some help form gusto’s blog and google (as usual). So, Lets do it now without spending more time.

First of all create a rails app and install tinyMCE rails plugin:
1) rails tinymce
2) script/plugin install git://github.com/kete/tiny_mce.git
3) rake tiny_mce:scripts:install

We will be using aspell utility to check spellings so install “aspell” first. You can do it by “apt-get install aspell” or “port install aspell”

Once it is done then add following two lines in your layout(application.html.erb):
1) <%= javascript_include_tiny_mce_if_used %>
2) <%= tiny_mce if using_tiny_mce? %>

Now consider that I have a Users controller and User model with a text field ‘about_me’, and I want to use tinymce with spellchecker for that field.

To convert about_me textarea(new user form) to tinyMCE, add following code to users controller:

**Please cross check that you have added(enabled) ’spellchecker’ in :plugins and added spellchecker button(I have added it in :theme_advanced_buttons2).

Now if you go to new user form you will see that about_me text area is replaced by tinymce editor with spellchecker button.

Now, add two more lines for spellchecker in above tinymce editor configuration:

1) :spellchecker_languages => “+English=en” (english language)
2) :spellchecker_rpc_url => “/users/spellchecker” (rails url where spellchecking will be done)

So our final configuration will be look like:

Now download Spelling.rb, save it as spelling.rb in your rails lib dir and change ASPELL_PATH at line 7 according to your aspell installation.

Once it is done include this module(”include Spelling”) in application.rb or you controlller (here in our case users controller).

Now, create an action in named spellchecker in you controller(’users’):

Now, Type some thing in tinymce editor and click on spellchecker button. It will highlight misspelled words.

2.png

And when you click on highlighted misspelled word it will show suggessions.

3.png

Enjoy

24 Apr 2007

wanna add keyboard shortcuts to your web application?

javascript, rails, web development posted by Akhil Bansal

Today I was looking for some javascript to add keyboard shortcuts to my project. And after little search on google I came across a very cool and lightweight javascript library shortcuts.js .
This library allows you to make keyboard shortcuts on a fly.
Example:

Please visit this site to know more about this javascript library.

03 Nov 2006

Animator.js: A javascript library by Bernie Sumption

ajax, javascript, web development posted by Akhil Bansal

While surfing on net I came across a javascript library(animator.js) based on prototype library written by Bernie Sumption.

I found this library a bit interesting. One that appeals me is the ability to define start state and end state through CSS

See in action here.