Saturday, January 30, 2010

Some useful resources for HTML 5 and CSS 3

I am feeling a kind of restlessness to try out some of the HTML 5 and CSS 3 features. I found couple of good links that I would like to share with you if you are starting to try out those features. I think some of tutorials are great for having a fine start. Happy coding guys....:-)

Searchable offline version of rdoc for Ruby 1.9 and Rails 2.3.5

If you are searching for an offline version of searchable rdoc for ruby on rails, then you can download it from here at Though it's on its beta period, but still it's kind of cool. Whenever I want to search anything I can do this instantly without waiting for the page to load specially in this part of the world where internet speed is not that fast... :-( Also, here is another offline version at Choose what suits your needs.

Tuesday, January 12, 2010

A basic scaffolding in your rails application

Hello folks, I am starting to build my own personal website. In the process, I have to build an application from scratch. I am choosing ruby 1.9.1 p243 and rails 2.3.5 as a starter. This is starting point for the application. I will be sharing my learning with you all as I go along. In this post I am starting off with creating basic scaffolding. Many of you have already done this I guess. I know there are some better approaches, but building from scratch will help me a lot and also some of you who are new to rails framework. So, let us begin.

Scaffolding is a nice approach if you are trying to get a grip on ROR projects. It creates all the basic pieces of model, controller, views and migration scripts for you. It will create basic set of functionalities for CRUD operations. If you are trying to introduce a new model Post with two fields title and body to your project, you then just have to run the script like this:
ruby {YOUR_RAILS_APP_DIR}/script/generate scaffold post title:string body:text
One thing to remember that when you are trying to use scaffolding, the migration script automatically generated will include the timestamp with it. If you don’t want that, you can turn it off. You can do this by adding
config.active_record.timestamped_migrations = false
to your config/environment.rb file. This will add incremental number instead of timestamp.

What if you want to delete the files automatically generated with scaffolding and regenerate for some reason? You can do that just by running the following command:
ruby {YOUR_RAILS_APP_DIR}/script/destroy scaffold post
Now, migrate your database using rake db:migrate command and start the server. Now type on the address bar http://localhost:3000/posts. Yah!! you guessed it right. It now supports all the CRUD operation on this model. That’s pretty easy, right? Though in real life application you will hardly use the view created by scaffolding, yet you can start off with it and change it the way you need.

ActionMailer invalid multibyte character error while starting WEBrick server

If you are using Netbeans for your Rails application development and you are using ruby 1.9, then when you want to run the WEBrick server you may get this error:

C:/Ruby19/lib/ruby/gems/1.9.1/gems/actionmailer-2.3.5/lib/action_mailer/vendor/tmail-1.2.3/tmail/utils.rb:117:in '': invalid multibyte character (ArgumentError)

Well this is not happening for your rails installation rather for a bug that exists in netbeans. The workaround is simple. You just have to add and set the variable to true in your {Netbeans installation dir}/etc/netbeans.conf file just like this:

netbeans_default_options = "-J-client -J-Xss2m -J-Xms32m -J-XX:PermSize=32m -J-XX:MaxPermSize=200m -J-Xverify:none -J-Dapple.laf.useScreenMenuBar=true -J-Dsun.java2d.noddraw=true"

You have to restart your netbeans to see the effect.

Friday, January 1, 2010

Running ferret server with mongrel and shared index folder

If you are using acts_as_ferret plugin for searching in your rails application then you must have faced some issues with indexing or running ferret server on your production server. In this post, I will share with you some tips about running and maintaining ferret server:
  • Running ferret server on production
  • Sharing your ferret index folder
  • Write a simple script to re-index all your data

Running your ferret server
If you have multiple instances of mongrel running in your production, then you must need to run the ferret server to handle situations when multiple processes are updating ferret index at the same time. If you want to run ferret server in your production server, then at first you have to open the port you set in ferret_server.yml of your config directory on the server you want to run it. acts_as_ferret comes with a built-in drb server which uses dRuby protocol. So, you should remember to open the port with other protocol (not http or something else). One very important thing is to change the following section of your /vendor/plugins/acts_as_ferret/lib/server_manager.rb as follows:
require(File.join(File.dirname(__FILE__), '../../../../config/environment'))
#require(File.join(File.dirname(ENV['_']), '../config/environment'))

Make sure you run the ferret server before running mongrel or apache. Now you can start the ferret server with the following command:
ruby /{YOUR_APP_DIRECTORY}/script/ferret_server -e production start

Also, the you can stop your ferret server with the following command:
ruby /{YOUR_APP_DIRECTORY}/script/ferret_server -e production stop

After you have started you ferret server you can start your mongrel and apache server. When mongrel is being started it checks for index folder in your app directory. If it does not find one it creates one. So, it’s possible that you will have different index folder with each deployment which is costly and re-indexing every time after your deployment is not at all a good solution.

Sharing your ferret index folder
You can set a default index folder in your production server which will be shared. To do this create a folder with read/write permission and create symlink to this folder each time you deploy and before starting the server. If you are using Capistrano for your deployment then make sure to run the symlink creation script before starting mongrel/apache. Otherwise the index folder will be created with the start of mongrel. A sample script could be like this:

run "ln -s /mnt/app/shared/ferret/ /mnt/app/current/index"

A simple script for ferret re-indexing
Here is a sample rake task for re-indexing your application data:
desc "Updates the ferret index for the application."
task :ferret_index => [ :environment ] do | t |
puts "Completed Index Rebuild of Model1 model"
puts "Completed Index Rebuild of Model2 model"
puts "Completed Index Rebuild of Model3 model"

To run rake task use the command:
rake ferret_index RAILS_ENV=environment

Make sure to restart your mongrel and apache server to point to the newer version of the index folder created within the directory you set after re-indexing is completed.