Web On Rails http://webonrails.com Exploring RubyOnRails Tue, 12 Aug 2008 10:31:17 +0000 #/?v=2.0.5 en Hoptoad: An Rails Exception Handling Service /2008/08/12/hoptoad-an-rails-exception-handling-service/ /2008/08/12/hoptoad-an-rails-exception-handling-service/#comments Tue, 12 Aug 2008 10:31:17 +0000 Akhil Bansal /2008/08/12/hoptoad-an-rails-exception-handling-service/ Many of you guys(as me) may have used Exception Notifier plugin to get Rails app exceptions right into your mailbox, and may also have faced some problem like this.

Also if you have 2-3 or more apps running in production then managing such exception mails is also a big headache. In such case one have to keep track of many things like which type of error is resolved/unresolved for which project etc… .

So, here is a good news for those who don’t know about Hoptoad. It is an hosted service by thoughtbot which receives your exceptions, notify you once per error type by email and keep track(resolved/unresolved, count etc…) of your errors on project basis.

By now its a free service. I’m gonna use this as my next project goes live. What abt you??? ;-P

Configuring TinyMCE SpellChecker with rails application /2008/08/08/configuring-tinymce-spellchecker-with-rails-application/ /2008/08/08/configuring-tinymce-spellchecker-with-rails-application/#comments Fri, 08 Aug 2008 12:14:26 +0000 Akhil Bansal /2008/08/08/configuring-tinymce-spellchecker-with-rails-application/ 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.


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



Hosting Rails app and Wordpress on same domain(as folder instead of subdomain) /2008/08/08/hosting-rails-app-and-wordpress-on-same-domainas-folder-instead-of-subdomain/ /2008/08/08/hosting-rails-app-and-wordpress-on-same-domainas-folder-instead-of-subdomain/#comments Fri, 08 Aug 2008 08:35:31 +0000 Akhil Bansal /2008/08/08/hosting-rails-app-and-wordpress-on-same-domainas-folder-instead-of-subdomain/ Hey guys, Yesterday I did an interesting server configuration. Actually we had a rails app hosted on server which is using passenger(a.k.a mod_rails). This application can be access by going to http://domain.com . Also we had a wordpress running which could be access by going to http://blog.domain.com.

But, for SEO sake I had to change configuration so that wordpress can be access by http://domain.com/blog instead of http://blog.domain.com/

The problem was if I configure wordpress for http://domain.com/blog and go to this url, the request was handled by rails app because of domain.com virtualhost.

So what I did? I changed apache virtualhost configuration for http://blog.domain.com and http://domain.com as:

Also I created a symbolic link to wordpress installation directory under rails public folder(ln -s /var/www/html/wordpress /var/www/html/railsapp/blog).

I restarted apache and it worked fine. Wordpress was running at http://domain.com/blog and rails app was as http://domain.com/.

Installing gem on Leopard /2008/08/07/installing-gem-on-leopard/ /2008/08/07/installing-gem-on-leopard/#comments Thu, 07 Aug 2008 11:47:38 +0000 Akhil Bansal /2008/08/07/installing-gem-on-leopard/ May be you guys have noticed that when you try to install a new gem on your machine it says some thing like “Updating meta data for 500 gems” and display one dot per gem. These dots moves very slowly and stops in some time.

I faced this situation many times and found a solution some where on net. I don’t remember the link but it worked.

According to that, you just need to add:
require ‘resolv-replace’
as the first require in /usr/bin/gem file.


When Ultrasphinx is used with polymorphic associations… /2008/08/07/when-ultrasphinx-is-used-with-polymorphic-associations/ /2008/08/07/when-ultrasphinx-is-used-with-polymorphic-associations/#comments Thu, 07 Aug 2008 08:56:04 +0000 Akhil Bansal /2008/08/07/when-ultrasphinx-is-used-with-polymorphic-associations/ :body, :include => [{ :association_name => ‘article’, :field => ‘title’, :as=> [...]]]> Lets first consider simple has_many and belongs_to associations as:

Now if you wish to index the title of associated article with comment for searching, you just need to add ” is_indexed :fields => :body, :include => [{ :association_name => ‘article’, :field => ‘title’, :as=> ‘article_title’}] ”

So, your comment model will look like:

Setup ultrasphinx by issuing:

Now at rails console try:

Simple, we have article and comment models with has_many belongs_to associations. Comments are indexed with their associated article title. So it can return comments if query matches with article’s titles.

Now, consider a case when we wish to change comment model to make it polymorphic. In that case our models will be look like:

Check at rails if associations are working fine:

Now, the point is to index article title with comments. Here we can get associated article using ‘commentable’ i.e. comment.commentable.

So, change ultrasphinx is_indexed code in comment model accordingly:

Note that we have changed associan_name to ‘commentable’.
Next, when we reconfigure ultrasphinx using ” rake ultrasphinx:bootstrap”(since we have changed db schema), it starts throwing errors:

After spending some time on research, I was able to make it work by making some changes in comment model:

Lets check it on rails console:

In such cases we need to define class_name and association_sql in is_indexed statement instead of association_name.

Hope it helps…

When Ultrasphinx is used with STI… /2008/07/31/when-ultrasphinx-is-used-with-sti/ /2008/07/31/when-ultrasphinx-is-used-with-sti/#comments Thu, 31 Jul 2008 14:06:38 +0000 Akhil Bansal /2008/07/31/when-ultrasphinx-is-used-with-sti/ Hi Guys, it has been a long time since I last posted. I had worked on several things since then, and have couple of posts pending/draft. One of those posts is related to Ultrasphinx, when it is used with STI models.

For those who are new to Ultrasphinx: Ultrasphinx is a rails plugin and client to the Sphinx(full text search engine) written by Evan Weaver. More about Ultrasphinx here.

Lets get into the situation. Consider a STI case where we are using ultrasphinx to index several fields:

Now assume we have following data in posts table:
and now in application root:

By this point we have created sphinx configuration file, indexed all records from ultrasphinx models and started sphinx search daemon.

Now open script/console and create and fire a query to find some articles:

Here when you run the query you get an exception, because it is trying to find an article with id 5, which actually a story type not article. So why it is trying to find such article?

Now have a look into config/ultrasphinx/development.conf file. Under section “source articles_main” you’ll get “SELECT (posts.id * 3 + 0) AS id, ‘’ AS article_title, posts.body AS body, ‘Article’ AS class, 0 AS class_id, posts.title AS title FROM posts WHERE posts.id >= $start AND posts.id <= $end GROUP BY posts.id" Which is a SQL to get record to index.

If you check it carefully you'll findout that it should select all articles record to index but unfortunately it is selecting all records from table and considering them as articles. To fix it you need to modify this query and add "posts.type = 'Article'" in where condition. So the query should be "SELECT (posts.id * 3 + 0) AS id, '' AS article_title, posts.body AS body, 'Article' AS class, 0 AS class_id, posts.title AS title FROM posts WHERE (posts.type = 'Article') and posts.id >= $start AND posts.id <= $end GROUP BY posts.id"

But still there is a problem. If you do this manually you have to do it again and again whenever you issue "rake ultrasphinx:configure" because this configuration file will be overwritten.

Better option is to add conditions in models as is_indexed options like:

and it worked fine.

Manik gave me an idea to write a patch for ultrasphinx to add such conditions automatically in case of STI. So may be another post related to ultrasphinx will be soon ;-)

Update: Here is the patch Git patch: Fix STI Issue. After applying this patch you need not to add conditions explicitly for such case. It will automatically check and add conditions for STI.

Git Error: trailing whitespace, indent SP followed by a TAB, unresolved merge conflict /2008/04/23/git-error-trailing-whitespace-indent-sp-followed-by-a-tab-unresolved-merge-conflict/ /2008/04/23/git-error-trailing-whitespace-indent-sp-followed-by-a-tab-unresolved-merge-conflict/#comments Wed, 23 Apr 2008 18:19:34 +0000 Akhil Bansal /2008/04/23/git-error-trailing-whitespace-indent-sp-followed-by-a-tab-unresolved-merge-conflict/ I have been using Git from last few days, and faced following errors while committing:

1) Trailing whitespace
2) Indent SP followed by a TAB
3) Unresolved merge conflict

The first error “Trailing whitespace” is because of carriage-return/line-feed(windows style line feed/end). To resolve this problem comment following lines(58-60) in .git/hooks/pre-commit file:

The second one “Indent SP followed by a TAB” is because of leading spaces/tabs. To resolve this problem comment following lines(61-63) in .git/hooks/pre-commit file:

The third one “Unresolved merge conflict” is because of seven or more successive occurrence of = or < or > characters. Major chances of having seven = character in README or doc files. To resolve this problem replace following line(64) in .git/hooks/pre-commit file:


These tricks worked for me, I hope it could help you.

Rails Plugin Annotate Models For Spec And Spec Fixtures /2008/04/23/rails-plugin-annotate-models-for-spec-and-spec-fixtures/ /2008/04/23/rails-plugin-annotate-models-for-spec-and-spec-fixtures/#comments Wed, 23 Apr 2008 11:18:14 +0000 Akhil Bansal /2008/04/23/rails-plugin-annotate-models-for-spec-and-spec-fixtures/ I have been using “Annotate Models” rails plugin written by Dave Thomas since I found it around one and half year ago. It really helps while writing fixtures. But if you use RSpec you might miss schema info at the top of your rspec fixture file as I do. So, today I modify the plugin file so that it prepends schema info to spec file and fixture file. Below are the links of patch file:

svn patch to add schema info to spec file and spec fixture file
git patch to add schema info to spec file and spec fixture file

Git - Fast Version Control System /2008/02/15/git-fast-version-control-system/ /2008/02/15/git-fast-version-control-system/#comments Thu, 14 Feb 2008 19:11:01 +0000 Akhil Bansal /2008/02/15/git-fast-version-control-system/ Git is getting popular in Rails community these days, as there were being many changes in rails to support Git.
Git is a open sourse fast version controller system. It was originally designed by Linus Torvalds to handle large projects. It was inspired by Monotone & BitKeeper. It is a distributed version controlling system. It gives you ability to commit, traverse into history while being offline. Actually every working copy of Git is a full-fledged repository. It has no central server like SVN. One can share his working-copy/repository by using various protocols like ssh, http, ftp etc. One thing I like about Git over SVN is that the size of Git’s repository, which is smaller compared to SVN.

I gave an introductory presentation in VinSol. I am sharing it with you, this is not very explanatory as I focused on speech more. Please share your views.

Migration: Adding/Removing columns are now much easier /2008/01/23/migration_adding_removing_columns_are_now_much_easier/ /2008/01/23/migration_adding_removing_columns_are_now_much_easier/#comments Wed, 23 Jan 2008 17:24:54 +0000 Akhil Bansal /2008/01/23/migration_adding_removing_columns_are_now_much_easier/ You may have noticed by now, that in Rails 2.0 changeset 7422, you can specify columns you want to add/remove in your migration by passing attribute:type pairs to the migration generator.

For example, lets assume that we need to add a column ‘role’ in users table(User model). In this case generate a migration like:


Here AddRoleToUser plays the main role. ‘Add’ specifies the we want to add column(s) and ‘User’ separated by ‘To’ specifies the table.

Similarly, if we need to remove a column ‘role’ :


Here RemoveRoleFromUser plays the main role. ‘Remove’ specifies the we want to remove column(s) and ‘User’ separated by ‘From’ specifies the table.

Isn’t it cool?

Ajax based online ruby API documentation /2008/01/14/ajax-based-online-ruby-api-documentation/ /2008/01/14/ajax-based-online-ruby-api-documentation/#comments Mon, 14 Jan 2008 18:10:07 +0000 Akhil Bansal /2008/01/14/ajax-based-online-ruby-api-documentation/ http://www.rubybrain.com/

Ruby Script for SVN commit notification with log message, list of updated files and readable colored SVN Diff /2008/01/14/ruby-script-for-svn-commit-notification-with-log-message-list-of-updated-files-and-readable-colored-svn-diff/ /2008/01/14/ruby-script-for-svn-commit-notification-with-log-message-list-of-updated-files-and-readable-colored-svn-diff/#comments Mon, 14 Jan 2008 14:17:16 +0000 Akhil Bansal /2008/01/14/ruby-script-for-svn-commit-notification-with-log-message-list-of-updated-files-and-readable-colored-svn-diff/ Some days ago I wrote a post about “SVN commit notification” which uses a perl script for sending commit notification with svn diff by mail. In this mail you can find svn diff from the last committed revision. I used to love this mail, soon I realized that it is a bit ugly and difficult to read. Also there were some important information missing. Like the name of user committing the code, the log message etc…

And then I started writing my own ruby script for same purpose but with some addition and modification. Commit notification script is that script, you can download and configure it with your SVN post commit hook as follows.

Add following line at the bottom of your post-commit file:

* Please remember to change the path of you commit-email ruby script.

Now open commit-email ruby file and modify the following section according to your requirement:

You are done with that, now onwards whenever someone commits the code, you’ll get the commit notification mail like:

Commit Email Preview

Presented at BarCampDelhi3 /2007/12/10/presented-in-barcampdelhi3/ /2007/12/10/presented-in-barcampdelhi3/#comments Mon, 10 Dec 2007 07:57:33 +0000 Akhil Bansal /2007/12/10/presented-in-barcampdelhi3/ Yesterday, I attended third barcamp in delhi. I presented a session “Deploying rails application in EC2″. It was an amazing experience. We talked about EC2 and I had given a demo “How to deploy rails application on EC2″.

I personally didn’t like some sessions. There were people from some company and after every 2 mins they were saying “we are the best”. I think this should not happen in barcamp. As far as I know about barcamp, it is for sharing your experiences, talking about new technology, but not for promoting your company.

I think that, at BarCamp there should be sessions on new things, not on the things that are popular and have similar other services.

There was a very good session by Jinesh Varia on Amazon Cloud Computing. He included Amazon’s simple storage system, and Amazon’s cloud computing(Ec2) in his presentation. He also helped me in answering people while the demo. He gave me a hint that very soon there will a news from Amazon that will surprise us.

I have uploaded my presentation slides on Slideshare.net:

Thanks to Impetus Noida for Hosting Barcamp for second time and Opera for Beer.

Third Delhi BarCamp /2007/11/21/third-delhi-barcamp/ /2007/11/21/third-delhi-barcamp/#comments Wed, 21 Nov 2007 08:27:04 +0000 Akhil Bansal /2007/11/21/third-delhi-barcamp/ I have a great news for you guys. We are planning Third BarCamp on Saturday, 8 December 2007, in Delhi.
Venue is not finalize yet, We have some options and shortlisting the final one.
We are also waiting for someone to take the sponsorship ;-)
Some people have proposed their sessions, I am proposing two with Manik. List of sessions(as of now):

    * Deploying Rails app on EC2 (Manik Juneja and Akhil Bansal)
    * Comparison of various Rails Hosting services. (Manik Juneja and Akhil Bansal)
    * Using Jmeter for Stress Testing your webapp
    * Desi Social Networking - Visual web ( Swagat Sen )
    * Everyday Productivity with Windows Live Tools (Abhishek Kant)
    * Gurugeeks - A call out for Web Geeks (Navjot Pawera)
    * Web Semantics and Accessibility (Navjot Pawera)

If you are planning to Attend/Present something you or want to help us in any way please visit http://barcamp.org/BarCampDelhi3 and add you name in appropriate section.

So see you in the camp ;-)

Two best online API/Rails API /2007/11/05/two-best-online-api-rails-api/ /2007/11/05/two-best-online-api-rails-api/#comments Mon, 05 Nov 2007 08:08:17 +0000 Akhil Bansal /2007/11/05/two-best-online-apirails-api/ Today I found two best API sites. One is http://www.gotapi.com.
This site provides APIs of almost all languages.

The other API site which is only focused on Rails is http://www.railsbrain.com/. I like this most. You can also download this API. It is AJAX based fast, useful.