Rails tip #2 How to add css class on rails form

At times its required to make pretty . Forms can be created using form_for or form_tag

To add css class to form_for

  form_for @user, :html=>{:class=> "foo"}

To add css class to form_tag

  form_tag({:action => 'new',...}, {:class => 'my_form'}) do

Happy Coding !!


Rails tip #1 – Set a custom layout for a web page

Its may require that you dont want to use application.html layout for some pages . For e.g display different header on page2 which than page 1 and the header is rendered in application.html.erb.

One way is to create a new layout for e,g headerless.html.erb inside a views->layout folder and in the particular controller which serves the request set the new layout .

 class SomeController < ApplicationController

    layout :headless , :only=>[:your_action1,:your_action2]


With the layout keyword you can use options such as :only, :except.

Rails Devise Gem , customize the sign_in path

While using devise gem , once the user logs in successfully , by default it gets redirected to the home page set in routes.rb via “root” . To redirect it to different page , add following code in¬†ApplicationController class

def after_sign_in_path_for(resource)
 "/welcome" #your custom page


Create complex json response , with child relationship in rails

For mobile based app, sometimes you want to return json responses to the client.

Let say I have a class with Attraction which could be typically a name of city and the city could have multiples places of attractions.

class Attraction
 include MongoMapper::Document

key :city, String
many :places

A child class Place.rb

class Place
 include MongoMapper::Document

belongs_to :attraction
 key :type, String
key :description , String
 key :address, String
 key :website, String
 key :rating, Float
 belongs_to :attraction



Note it will not change for ActiveRecord class.

Now for call like /attractions.json i want all the attractions and also places mapped to attraction.

class AttractionsController < ApplicationController
 # GET /attractions
 # GET /attractions.json
 def index
 @attractions = Attraction.all
 render :json => @attractions.to_json(:include => [:places])



:include  does the trick . Also if you want to include only selected fields from places class

render :json => @attractions.to_json(:include =>

[:places =>{ :only =>[:website,:address])



Rails 3 + MongDB , MongoMapper beginner tutorial

I recently used MongoDB at my work for mobile app and was tempted to try more. So i wrote a simple app in rails to play with it..

Lets assume rails is already installed.

References for tutorial:
MongoDb installation
MongoDB with Rails
Railcast of integrating rails with Mongo

The installation of MongoDB can be done using the first reference.
Lets create a rails project now to record daily expenses.

rails new expense_record --skip-active-record

We need to update the gemfile by adding mongo dependencies:

require 'rubygems'
require 'mongo'

gem 'rails', '3.0.10'

# Bundle edge Rails instead:
# gem 'rails', :git =&gt; 'git://github.com/rails/rails.git'
source 'http://gemcutter.org'

gem "mongo_mapper"
gem "bson_ext", "1.3.1"
gem "bson" ,"1.3.1"
gem  "mongo","1.3.1"

Now run

bundle install

Create a new filemongo_config.rb in config/initializers folder.
Lets add the following code :

MongoMapper.connection = Mongo::Connection.new('localhost', 27017)
MongoMapper.database = "#expense-#{Rails.env}"

if defined?(PhusionPassenger)
   PhusionPassenger.on_event(:starting_worker_process) do |forked|
     MongoMapper.connection.connect if forked

NOTE: here the #expense is the name of the database. By default Mongo listens to 27017 port. Also the database name should not contain “.”, such as “expense1.0” is wrong.

Now lets create an expense model with field “expense_name” “date_spent” “amount”.

rails g scaffold expense expense_name:string date_spent:string amount:integer --skip-migration --orm mongo_mapper

All the classes are created as expected except the model class expense

class Expense
  include MongoMapper::Document

  key :expense_name, String
  key :date_spent, String
  key :amount, Integer


Instead of ActiveRecord , MongoMapper is used which behaves pretty much the same.

Now we are good to run the project.

rails s 
<a href="http://localhost:3000/expenses"></a>

Happy coding..