Tuesday, August 18, 2009

Installing Ruby on rails in four easy steps

Ruby on rail is used for web application development.
Ruby is object oriented language.
Rail is framework that uses Ruby language to develop robust and scalable web application.
Ruby on rail installation is simple, newbie however find it a bit tricky first.
Here in this article I am going to tell you how can Ruby on rails be installed in windows XP.

1. Download latest version of Ruby from http://rubyforge.org/projects/rubyinstaller/
2. after the installation double click the exe file. this will start setup wizard which will guide you to the end.
3. once Ruby is installed next step is to set path
=>start => control panel => system =>advance => environment versiables
click path and press edit.
add
;C:\Ruby\bin


at the end of the path, I assumed that you have installed Ruby in drive c.
press oK.
4. now in command prompt

write

gem update --system


when the process completed, write

gem install rails


you have done. now its time to create your first project

in command prompt, write
rails firstproject


Once the projec is create, in command prompt, write

cd firstproject
ruby script/server


now browse

http://localhost:3000/

you will see ruby on rails welcome page.

Public transport some facts

Public transport (also public transportation or public transit or mass transit) comprises passenger transportation service which is used by public [1].
An important role played by public transport in the society is to enable all the member of the society to travel, not only those having driving license and have access to their own automobiles.
Public transport like all other services and facilities in the third word countries is a real dilemma. Elite, which mostly uses their own private automobiles and vehicles, like all other public institutions, organization and facilities, rarely consider improvement in these services. As they never ever use it, so why care about it?
The fact however is, if government gave proper resources and made some improvements, public transit can play a very vital role in generating fair amount of revenue.
Traffic jam which is a real problem in the big cities can be decreased be providing well-designed transport system which is considered more dense then private transport.
I have been using public transport in my beloved country for the last two year, and can say that it has made me mentally sick.
People behave like animals. I am not blaming them for behaving like this, as they are put like animals in buses. They don’t care walking on your feet and pushing with their hands.
Young people specially students stand near entrance, making it hard for the people to get in and out.
Situation become even more deteriorating when a person with his bag selling books, handkerchiefs, toffees, and homeopathic medicines arrived in crowd and start speech explaining benefits of the product he owns.
Beggars also contribute to the noisy environment, trying hard to collect money for no work.

Tuesday, August 11, 2009

Dojo: Creating image using dojo script

Dojo provides numerous functionality like:
1.Easy and smooth creation of different widget such as dojo grid, forms, datapicker, calendar etc.
2.Prolific method of interacting with html elements.
3.Achievement of ajax functionality with far more less code.
In this article I am going to discuss how to create an image element using dojo script and attach that image to certain html element.
In order to achieve functionality on loading the page, you will need to use dojo.addOnLoad() method. This takes the following form.
<script>
dojo.addOnLoad(function(){
alert(‘called on load of page’);
});
</script>


Now in the next few lines we are going to put the actual functionality.
<script>
dojo.addOnLoad(function(){
dojo.extend(dojo.NodeList, {
addImage: function() {

return this;
}
});
});
</script>

The most important things here are extending dojo.NodeList and then adding addImage function that initially has nothing, but we are going to add real functionality to it later on. This addImage method will contain the functionality for creating an image.
The question here however is, why we extend NodeList.
Well this is pretty simple. If you want to achieve functionality like;
dojo.query(‘input[id=name]’).addImage().removeClass(‘complusory_image’);
you will need to extend you method from NodeList. Doing so will enable you to access entire NodeList method in the way above.

Don’t worry about dojo.query() method, I will explain it shortly for you.
Now lets put the real script for creating img tag in our addImage method.
var img = dojo.doc.createElement('img');
dojo.attr(img, {
src: "path/to/imges/img.png",
alt: "This is my simple image",
style: {cursor: "pointer"}
});

In the first line we have created img tag. In the second line we have attached different attributes to the img. You can define as many valid attributes associated with img tag as you wish.

The code in the addImage function looks, however, a bit different. See the following code.
<script>
dojo.addOnLoad(function(){
dojo.extend(NodeList, {
addImage: function(args){
this.forEach(function(n){
var img = dojo.doc.createElement('img');
dojo.attr(img, {
src: "path/to/imges/img.png",
alt: "This is my simple image",
style: {cursor: "pointer"}
});
dojo.place(img, n, "after");
});
return this;
}
});
});
</script>

The function for addImage contain args as parameter. This means that the image tag created can be attached to as many html elements as you like. In the forEach loop we create image tag and attach at to the arguments passed to it.
Now in the dojo.addOnLoad function, at the end, write the following script.
dojo.query(‘input[id=name]’).addImage();

The above script states that attach an image to input tag having id equals to name.
It you want to attach element to all input tags on your page, write
dojo.query(‘input’).addImage(); 

The final script on my page look like this.
<script>
dojo.addOnLoad(function(){
dojo.extend(NodeList, {
addImage: function(args){
this.forEach(function(n){
var img = dojo.doc.createElement('img');
dojo.attr(img, {
src: "path/to/imges/img.png",
alt: "This is my simple image",
style: {cursor: "pointer"}
});
dojo.place(img, n, "after");
});
return this;
}
});
dojo.query("input[id=name]").addImage();
});
</script>

Friday, July 31, 2009

Creating dojo datepicker with dojo script

Dojo date picker is nice and beautiful widget. The nice thing about any dojo widgets is that they can be easily attached to appropriate html tag. To assign date picker to input box take the following cod.


<html>
<head>
<script src=”path/to/dojo.js”>
require(‘dijit.form.DateTextBox’);
</script>
<style type=text/css>
@import "path/to/dojo/dijit/themes/tundra/tundra.css";
</style>
</head>
<body class= “tundra”>
<input type=”text” dojoType=”dijit.form.DateTextBox” name=”dd” id=”dd”/>
</body>
</html>



If you correctly include dojo.js and tundra.css, you will see a nice

Read More

Monday, July 27, 2009

Zend Framework, dojo and Json

I have already written about json in one, actually some of my articles. Cool functionality can be achieved using json. Thanks to http://www.json.org, http://www.zend.com and http://www.dojotoolkit.org for provide easier solutions.

I'm not gonna talk what these three are all about, but instead share a little secret about these.
Json creation in dojo is as simple as this.
var arr = new Array();
arr[0] = “apple”;
arr[1] = “ball”;
arr[2] = “cat”;

dojo.toJson(arr);


Using Zend to convert array into json took only one line of code.
Zend_Json::encode($arr);

And decoding- converting json to array, is as simple as this
Zend_Json::decode(json_string);

Tuesday, July 14, 2009

Apache: Setting virtual host on localhost

It was up to this task since yesterday. I haven’t done this before, setting sub domain- virtual host on local system. I tried a lot yesterday but in vain, however when I came to office today I configured it successfully.
As I was stuck in the middle and feel lot of pain, so I better write it for those facing the same issue.
Why I was up to this task?
Well, actually my client was facing problem and reported that session is not working properly on domain and sub domain. So I was having no way other than setting virtual host.
However before leaving yesterday, I talked to my client that I am unable to solve this problem. He told me that I shouldn’t be worried about it because he will take care of this issue. The more important thing is that when I arrived today, I found that the problem has already been solved. Thanks to him for giving my favor.
Thought the problem has been solved, but still is worthy to write a small post here because I have successfully solved the problem.
Before going to discuss it, I would like to tell you that the configuration has been taken place on window so the paths may not be same.
Let have a look.
First of all you will need to open.
C:\xampp\apache\conf\extra\httpd-vhosts.conf

I assumed that you have install xampp.
And add following lines to httpd-vhosts.conf file.

NameVirtualHost *:80


<VirtualHost *:80>
ServerName localhost
DocumentRoot "C:/xampp/htdocs/"
ServerAdmin admin@localhost
</VirtualHost>


<VirtualHost *:80>
ServerName mysite.com.localhost
DocumentRoot "C:/xampp/htdocs/mysite/"
ServerAdmin admin@mysite.com.localhost
<Directory "C:/xampp/htdocs/mysite/">
Options Indexes FollowSymLinks
AllowOverride FileInfo
Order allow,deny
Allow from all
</Directory>


Here I have defined server localhost pointing to my default htdocs directory and another host mysite.com.localhost pointing to "C:/xampp/htdocs/mysite/".
Another important thing is
Here you would need to specify the port. In my case its 80.
If you don’t specify the port on which apache listen, apache may take you to the same page for both the server. So be careful.

Next go to your Window/system32/dirvers/etc/ and open hosts file.
You will find
127.0.0.1       localhost

At the end of the file. Add following line next to it.
127.0.0.1    mysite.com.localhost

Reboot your system and you are done.

Method chaining in object oriented programming

In my previous article I discussed some of the nice thing of object oriented programming. Inheritance –creating sub classes from inheriting parent, contain all the attributes of the parent class plus its own. Polymorphism means different form, like overloading and overriding methods and dynamic binding- deciding which class in the inheritance tree should be called on runtime, are techniques that give great benefits to programmers.
In this article I am going to show you a simple but very useful trick while using “this” keyword for method chaining. “this” when use in the class, refer to the same class.
Consider the following example.
class Person {
protected $_name;
protected $_age;
protected $_gender;

public function setName($name) {
$this->_name = $name;
}
public function getName() {
return $this->_name;
}
public function setAge ($age) {
$this->_name = $age;
}
public function getAge() {
return $this->_age;
}

public function setGender($gender) {
$this->_gender = $gender;
}
public function getGender() {
return $this->_gender;
}

}

In object oriented programming it’s always a good practice to define getter and setters. In php you can define __set and __get as setters and getter.

To create an object of the above class and define person properties, we will need to write the following code.
$person = new Person();
$person->setName(“person_name”);
$person->setAge(24);
$person->setGender(“male”);


And now If you want to get these values, write
$person->getName();
$person->getAge();
$person->getGender();


Well, if you want to set values using a single statement, you will need a code similar to this.
$this->setName(‘person_name’)->setAge(24)->setGender(“male”);

To achieve this, you will need to write the following statement in all your setters.
public function setName($name) {
$this->_name = $name;
return $this;
}

The only thing we need here is “return $this”. This statement return a reference to the same object.
Place this statement in your setAge() and setGender(), and now you can write
$this->setName(‘person_name’)->setAge(24)->setGender(“male”);

for setting all the three properties.
Any suggestion is welcome. Cheers