Creating direction from source to destination in Google Map using Rails

Google map in Rails application is implemented by YM4R  gem/ ym4r_gm plugin. Suppose anyone one to see direction in Google Map From his address to destination address by clicking his city point in Map. The following code in Ruby on Rails do that with a bit of JavaScript function...........
def initiate_map fromadd , toadd
@map ="map_div");
@map.control_init(:large_map  => true,:map_type => true)
@map.center_zoom_init([38.957101, -95.251469],4)
@map.event_init(@map, :dblclick,"function(marker,address){ if (marker)
{map.removeOverlay(marker); } else {
var gdir;
gdir  = new GDirections(map, document.getElementById('directions'));
gdir.load('from: ' + '#{fromadd}' + ' to: ' + '#{toadd}');
}}" )
Now for dynamically updating the direction do the following code….

def initiate_map1 fromadd , toadd'map_div')
@js_str = "var geocoder = new GClientGeocoder(); \n"+
"geocoder.getLatLng( '#{MappingObject.javascriptify_variable(fromadd)}',function(point) {
map.setCenter(point, 13);
var marker = new GMarker(point);
}) ; \n" +
"GEvent.addListener(map, \"click\", function(address) { var gdir  = new GDirections(map, document.getElementById('directions'));
gdir.load('from: ' + '#{MappingObject.javascriptify_variable(fromadd)}' + ' to: ' + '#{MappingObject.javascriptify_variable(toadd)}');});"

After that you have to write  page << @js_str in a js tempalte. In the view write this code

=@map.div(:width => 600, :height => 400)

All is done. 

My reading blog list…..

SM Sohan’s blog for some interesting things of Rails =>

Samiron’s blog for creative talks =>

How to create procedure and passing argument as code block ??

In Ruby a Proc is an object that holds a chunk of code. We can create Proc by ” lambda( )” method. To call the code block we have to use call method of Proc.

And if we want this Proc object to pass as code block argument then we have to append an ‘&’ with Proc name.

One thing to remember that we have to associate an ‘&’  with the parametername to pick up any code block in the method definition. The whole thing is as follows —-

class Report

def output_report (&Outputblock)  )


# code-block here with Proc

hello = lambda do

puts('I am inside a proc')

report = Report .new

report.output_report( &hello)



In the above code “hello” is the created procedure.

Auto-escaping HTML with Rails in the model level for protecting script injection

We can sanitize an input field data easily before saving or before displaying it in Rails. In this case we don’t need to h function in the view level. We can do it at each model by using plugin.

xss-terminate is a plugin  that makes stripping and sanitizing HTML stupid-simple, => , here we can find plugin. But currently it can’t be installed directly, so we have to download it and unpack its contents in a folder named “xss_terminate” under vendor/plugins folder.

Suppose we want to sanitize any script before saving an user name in user model so we have to just write the following code……

class User < ActiveRecord::Base
xss_terminate  :sanitize=>[:user_name]



We can also avoid any field from sanitizing by “:except” option as follows..

xss_terminate  :except => [:address]

More on…..

How we do A/B testing in Rails


For A/B testing in Rails there is a gem named vanity. At first we have to install the vanity gem by the command=> gem install vanity. After that we have to just configure the test.rb file under the “/environments” folder as follow…

config.after_initialize do
require "vanity"

Be careful this code must be written at the end of the file. Here we used Vanity.playground.test!  for not accessing the redis for storing data. But if we want to access redis then we have to configure as follows…

Vanity.playground.redis = "::15" # Different DB
Vanity.playground.redis.flushdb # Do this before running tests

Add a new controller for Vanity:

class VanityController < ApplicationController
include Vanity::Rails::Dashboard

Lets write a test code  ===>

Now our A/B test file should be under the “experiments/” folder. Lets see a sample code

ab_test "Customer Message without Pricing" do
 description "Testing to see if messages without pricing increases number of signups."
 metrics :trial_activation

Here the name of the metric parameter is same as the name of the file where our actual experiment is done under the “experiments/metrics/” directory. The following code is contained in the trial_activation.rb file.

metric "Signup for trial" do
 description "Measures how many people signed up to try our awesome service."
 model Account.in_trial

Quick helper links…………….

Important links……

Basic about Object Oriented JavaScript

JavaScript has nice and special object-oriented features. Discussion about some of these features are…….

(1) JavaScript Object

Object in JavaScript is not same as C#, Java. JavaScript object is called a name-value pair i.e just like {“name” : “value”}. A function in JavaScript is basically an object function. Example…..

  • var motor = { “Name” : “Honda” };
  • alert( motor.Name ); // output is Honda.

(2)Instantiating Object

In JavaScript an object is instantiated by calling function with the new operator. This is done by following way…..

  • function Car(name) {
  • }
  • var motor = new Car(“Honda”);

(3) Prototype object

An object is a copy of existing example (prototype) object. Any properties and method of the prototype object also appear in the created object. That means created object inherits the properties and method of the prototype object. Every constructor function/object has property name “prototype” and this prototype object has a property called “constructor”. Constructor property indicates the base constructor function. One thing need to remember that “constructor” is not the own property of created object. In JavaScript “Object.prototype” is the ultimate base prototype of  all prototypes.

(4)Static members

A member declared in a function object  associated with the function name is called static member of the object. The calling of the static member/function is same as Java.

  • function Car() {
  • function(){
  • return “Motor”;
  • };
  • }

(5)Concept of closure

Anonymous inner function is treated as closure in JavaSript because the inner function closes over the environment (the arguments and local variables of the outer function) in which it is defined. Closure in JavaScript is used for simulating private members. See the following….

  • function Check (upperbound) {
  • return function (lowerbound ) {
  • return (upperbound>lowerbound) ? true : false;
  • };
  • }
  • var bigger =  Check (100);
  • alert(bigger(90));  // return true.

(6) Accessing Private Members

Normally local variables of a function can’t be accessed outside the function. But in JavaScript local variable can be accessed through inner function’s closure.

(7) Inheriting classes

Properties of one class(prototype) is inherited by another class(prototype) only by constructor function. Lets see it in following…..

  • function vehicle {
  • //
  • }
  • function car {
  • //
  • }
  • car.prototype = new vehicle();

Here, car inherits the vehicle class.

(8) Namespaces

Namespaces in JavaScript is used for creating JavaScript library. In the following way we can build Namespaces for prototype classes…..

  • var namespace={ };
  • namespace.ClassPrototype = function() {
  • //
  • };

Read the rest of this entry »