Tuesday, September 25, 2007


Lightbox is a simple script used to overlay images on the current page.
It gives the fancy visual effects and provides the functionality to create album of related images and allows navigation through them with ease.

Setup for lightbox -:

1. It uses “Prototype” javascript library and “Scriptaculous” javascript library(for visual effects).So you have to include this 2 libraries in your tempalte header.

(javascript library of lightbox)
(This will give all the effects in scriptaculous.)
(This will include all the javascripts required for all ajax effects in RoR)

2.It requires CSS of lightbox.

(CSS for lightbox)

3.Check CSS and javascript for “path of the defult images” i.e prev.gif, next.gif,loading.gif,close.gif.

Activate lightbox -:

1.To give the lightbox effect add “rel=lightbox” in the image tag.
href=”/images/image-1.jpg” rel=”lightbox” title=”my caption”>image #1

2.To have the group of related imges add rel=”lightbox[roadtrip]” in the image tag.

href=”/images/image-3.jpg” rel=”lightbox[roadtrip]”>image #3

Download Lightboxv2.03.3 -:


The most important point-:

On every page load Lightbox automatically initialised by ‘initIightbox()’ function written in ‘lightbox.js”.Lightbox need to initialise every time to see its effects.

Thus render partial of the page does not give the lighbox effect because it does not call ‘initIightbox()’ function written in ‘lightbox.js”.so this can be solved by calling onclick =”initIightbox();” for an image.

for example-:
href=”/images/image-3.jpg” rel=”lightbox[roadtrip]” onclick=”initLightbox();”>image #3

Enjoy !!

..Naveen Joshi

Ajax Pagination

This pagination facilates retrieving of photos from database with AJAX.

1.Create partial in views.Add following code to it.

0), :update => “your div id“,
:url => {:action => :imgstrip, :id => post, :skip => ([0,skip.to_i-1].max) } if post.medias.size>4 –>

break if (cnt+=1)==5

0), :update => “your div id“,
:url => {:action => ‘action‘, :id => post, :skip => ([0,[skip.to_i+1,post.medias.size-4].min].max) }if post.medias.size>4 –>

Replace “post” with “your object“

2.To render template add following line to views

render :partial => ‘template‘, :locals => { :post => post, :skip => params[:skip] }

3.Add following in controller.rb

def action
post = Post.find params[:id]
render :partial => ‘template’ :locals => { :post => post, :skip => params[:skip] }

Replace ‘template’ with your view name.
This will show four photos after each scroll.


Naveen Joshi

Wednesday, June 13, 2007

Maintaining an object between two pages

I wanted to write a form where users can create or edit a claim. One of the fields is a company. Since there is an internal code for each registered company, I wanted the users to search by company name. On the form, I plan to add a link called "Search Company". This link will take the user to another form to find a company by name, and the matching ones will be displayed as links. If the user click on any of the links, the claim form page will be redisplayed with the selected company and the rest of the data the user has entered. Right now, I had a table called claims and a model that matches it. In Java Servlet Faces, I use

to pass the objects to the next page, but I was not sure how to do it on Rails. After digging around forums I found the following


There's always several ways to get the skin off the cat, but just save the id of the selected company in the "session". Something like this in the action that fires when they click on a company...session
:selected_company_id] =Company.find_by_name(params[:selected_company])

(this assumes you are returning the selection as "selected_company")

Enjoy :)


I was searching for a utility for deploying web applications. I found Capistrano , which is a utility for deploying web applications. I decided to try my hands with it, and my experience says It is ideal for system administration.

Advantages of Capistrano configuration -:
1.It can execute commands in parallel on multiple servers.
2.It allows to define tasks which can include commands that executed on the servers.It allows to define task specific roles for server.
3.It includes some basic templating allowing to dynamically create and deploy things like maintenance screens, configuration files, shell scripts, and more.
4. It uses repository which allows easy upgration of sorce code.

For Rails users its cool cool shot....
It can perform migrations during deployment.It can restart mongrels/fcgi.
It allows to use transactional commands.When any of the command fails changes are rolled back automatically.

To configure it following specifications are required-:
It requires SSH to connect to the servers.
The remote server is capable of understanding POSIX shell commands.
It requires subversion to manage your source code.
It require FastCGI to power your application.

After spending so many hrs I like following features -:
1.It facilitates to keep production database password secure by allowing to create host specific database yml.
2.The"sudo" helpers got a lot more powerful; allows to pass a specific set of hosts to them or even call them outside of any task by implementing capistrano scripts, basically making an entire file a single task.
3.It allows to create own deployment strategies from multiple server configuration by allowing selection of servers with their environment variables

Tuesday, June 12, 2007

Multiple Tables

Recently I had a problem with real life webapps withmultiple tables in a DB where some depend on others. I had a app with 3 tables. 2 of the tables are directlyrelated meaning that i can't insert into one with out inserting thecorresponding values into the other. I figured out with somehelp from IRC how to do this. but needed to have validattion on both models before the inserts are made. what was happening is that if validation passes on the 1st, that inserts then the 2nd fails and that doesn't insert. So question is how do i handle such a situation.

Answer , I found after some research and discussing in Forums

I Read up on validates_associated[1]. and consider:
order = order.new(params[:order])
line_item = table2.new(params[:line_item])
order.line_items << line_item
something went wrong

This is 'air code' and clearly a simplistic example, but what should happen here is that you create a new order. orders has an associatedtable, line_items (probably a has_many relationship). When you use save!, an exception is created if there is a problem such as model validation failure. Because you used validates_associated, this bubbles failures in the associated item up. When the exception happens, the transaction block causes a rollback, leaving the database in its original state. The Rails dox [2] say that "save anddestroy are automatically wrapped in a transaction."Best to test this in script/console and observe its actual behavior.

.[1] http://railsmanual.org/module/ActiveRecord::Validations::ClassMethods/validates_associated[2] http://railsmanual.org/module/ActiveRecord%3A%3ATransactions%3A%3AClassMethods

Thanks Guys

Captcha Plugin

Recently I was creating a Ruby on Rails site and was curious about the best way tostop spam on the site. For account creation there is
:a) Captcha
b) email verification

For messages, forums I am sure you could try to check the message forany weird characters or typical spam phrases. I saw in the other post there is the hivelogic email enkoder for hiding email addresses forprotecting email addresses. I was reading a blog and it said captchaa nd email verification are bothersome to users. Almost seemed from theblog that it shouldn't be used. If that is true, what are RoR developers using? What is the best practice for Ruby on Railsdevelopers?

And finally found the answer at

Thanks Jason, Great Post


Different hosting companies have different settings on their host, I use Site5, so sharing my experience about the same

1) Open SSH client PUTTY and and go at the root of your personal directory (place where you can see the directory public_html).2) Using FTP, upload the following directories:* app* public* configThey should contain all the files created (but upload also the other directories if you have created files in other dir than the ones listed)3) Now let's create symbolic link,ln -s /home/{username}/apps/site/curent/public public_html4) We have to declare the symlinks by creating the .htaccess file:vi public_html/.htaccessand following lines of code,Options +FollowSymLinks +ExecCGIRewriteEngine On5) Next step is to modify the permissions (make the files executable)Code:chmod -R 755 your_app/*6) Edit your public/dispatch.cgi, public/dispatch.fcgi file and replace the first line like this:replace #!/usr/bin/ruby18 with #!/usr/bin/rubyRestarting dispatch.fcgi,pkill -9 -u thezixo -f dispatch.fcgiAt this stage you may want to test the installation by visiting the address:
or http://www.your_domain.com:3000/your_other_app_name/You should obtain the congratulations page of the server running perfectly well.7) Once you're done testing this, we're gonna create a simple route to display a page as the index page (like an index.html or index.php page)First delete the index.html in the public dir unless you want to use it.rm your_app/public/index.htmlthen,vi your_app/config/routes.rbthen,map.connect '', :controller => 'your_controller', :action => 'action_to_be_seen_as_index'8) Finally, edit the environment.rb file to put the app into production
mode:vi your_app/config/environment.rbthen
add the following line before the RAILS_ENV declaration, like this:ENV['RAILS_ENV'] = "production"RAILS_ENV = ENV['RAILS_ENV']Now, you should be allright and ready to show your work to the rest of the world !You will be able to access the index page by