Friday, June 18, 2010

How to create virtual hosts in Windows (wamp) web server?

To create virtual hosts or virtual domains in your Apache server on Windows using WAMP server is just a matter of editing few files. Find the walk-through below.

Prerequisites : Apache server for Windows

Step 1 – Setting up the ‘host’ file

1. Find the ‘host’ file in ‘C:\WINDOWS\system32\drivers\etc’ folder (or where you installed windows)
2. Open it with Notepade or any text editor.
3. You will see following lines

# Copyright (c) 1993-2009 Microsoft Corp.
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
# For example:
# 102.54.94.97 rhino.acme.com # source server
# 38.25.63.10 x.acme.com # x client host
# localhost name resolution is handled within DNS itself.
127.0.0.1 localhost
::1 localhost

4. Add the desired domain names in the end of the text (after the default localhost settings indicated above) it can be anything with or without extension (see the examples below)

127.0.0.1 mydomain.local
127.0.0.1 www.mydomain
127.0.0.1 mywebsite
127.0.0.1 subdomain.mydomain.local

Step 1 is done.

Step 2 – Configuring the Apache ‘httpd.conf’ and ‘httpd-vhosts.conf’ files

1. First we’ll enable a configuration file located in the WAMP server Apache folder. For this, open up the ‘httpd.conf’ file from the the “ c:\wamp\bin\apache\apahce2.2.11\conf” folder.
2. Find the below lines and delete the # key in front of the second line to un-comment and enable it.

# Virtual hosts
Include conf/extra/httpd-vhosts.conf

This will include the particular configuration file where we’ll setup the virtual hosts and their folders.
3. Now we’ll find the ‘httpd-vhosts.conf’ file located in the Apache server. Usually we can find it in “c:\wamp\bin\apache\apahce2.2.11\conf\extra\” in the WAMP server (replace the apache version number after the \apache\ folder)

Add these lines in that file to enable the virtual hosts (which we created in step 1)

NameVirtualHost 127.0.0.1
# This line will make virtual host based on names not IPs

DocumentRoot "c:/wamp/www”
#this is default root for websites in WAMP
ServerName localhost
#this is default localhost domain


Now again we’ll add another block of virtualhost which will point out custom domain this time.


DocumentRoot "c:/wamp/www/mydomain.local"
#We’ll create a folder inside /www/ folder for our domain files.
#No restrictions in the folder name but we’ll keep it same to make it
#easily identifiable.
ServerName mydomain.local
#this is our custom domain we added in the first step


So, the final look of the file will be like this:

NameVirtualHost 127.0.0.1


DocumentRoot "c:/wamp/www”
ServerName localhost



DocumentRoot "c:/wamp/www/mydomain.local"
ServerName mydomain.local


We can add virtual hosts as many as we needed using the same block. Once everything is added in the ‘host’ file as in step 1 and in the apache config files as in the step 2, we’ll have to restart the WAMP server to take effect new changes we’ve done. Click the WAMP server icon in the system notification area and chose ‘Restart All services’.

Thats it. Now you can access the domain by typing ‘http://mydomain.local’ in the address bar of your favorite browser.

Work locally with IIS and virtual hosts on windows

Here it shows how you can easily create virtual hosts on a windows machine to run multiple sites simultaneous each one having its own domain

Prerequisites : IIS Server for Windows

Steps:
1)Need to add instance in hosts file resides in C:\WINDOWS\system32\drivers\etc path. Add instance 127.0.0.1 mytestsite.local in host file.

2)Then need to start your server like ruby script/server -p 3000

3)Now run site using URL mytestsite.local:3000

Thus newly created instance can execute on custom url.

You can add multiple instance also.
e.g 127.0.0.1 mytestsite.local
      127.0.0.1 new.mytestsite.local

Friday, May 21, 2010

Delete old records from database if the number of records reached to max length

I need to maintain fixed number of records. While my method is going to insert a new record then the old record will be delete automatically if the number of records reached to max length.

Max_length = 4
def create
@students = Student.new(params[:student])
if @students.save
if Student.find(:all).length >= Max_length
@old_students = Student.first
@old_students.destroy
end
end
end

Thursday, May 20, 2010

Disable right click using javascript

<script>
var isNS = (navigator.appName == "Netscape") ? 1 : 0;

if(navigator.appName == "Netscape") document.captureEvents(Event.MOUSEDOWN||Event.MOUSEUP);

function mischandler(){
return false;
}

function mousehandler(e){
var myevent = (isNS) ? e : event;
var eventbutton = (isNS) ? myevent.which : myevent.button;
if((eventbutton==2)||(eventbutton==3)) return false;
}
document.oncontextmenu = mischandler;
document.onmousedown = mousehandler;
document.onmouseup = mousehandler;
</script>

Saturday, May 15, 2010

Specifying Document Compatibility Modes

To specify a document mode for your Web pages, use the meta element to include an X-UA-Compatible http-equiv header in your Web page. The following example specifies Emulate IE7 mode compatibility.

<html>
<head>
<!-- Mimic Internet Explorer 7 -->
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" >
<title>My Web Page</title>
</head>

<body>
<p>Content goes here.</p>
</body>
</html>

This example tells Internet Explorer 8 to display a Web page the same way that Internet Explorer 7 displayed it.

Friday, May 7, 2010

Delegation with Rails

Why delegation?

Before going to detail let me start with an example , we have a two models named User and profile having has-one association.

class User < ActiveRecord::Base
  has_one :profile
end

class Profile < ActiveRecord::Base
  belongs_to :user
end

Suppose Profile have attributes like first_name, last_name, phone etc and @user is the instance of User class.now to find name or phone of an user we have two ways -

1. Using dot-magic

FirstName = @user.profile.first_name

This is simple but becomes bulky for long chain associated object.For example @user.sth.sth.sth.sth…some_attribute

2. Using delegation

Instead we can delegate desired attributes in User model. Here is the code to add

class User < ActiveRecord::Base
  has_one :profile
  [:first_name, :last_name, :phone, ...etc.].each {|attr| delegate attr, :to => :profile}
end

Lets see usage of delegation in depth

Delegation in Rails

In ruby on rails Delegation provide us a fine way over dot-magic. Using delegation it is easy to access associated object’s attributes. Delegation is a feature Rails introduced in it’s 2.2 version.The concept of delegation is to take some methods and send them off to another object to be processed.Delegate simply delegates a method to another class.This is useful if you have a lot of cases where you are referring to fields in associated classes.

Let me explain this with a brief example:

Suppose you have a User class for anyone registered on your site, and a Consumer class for those who have actually placed orders:

class User < ActiveRecord::Base
  belongs_to :consumer
end

class Consumer < ActiveRecord::Base
  has_one :user
end

As for now, if you are in a Consumer instance, you can get their User information doing @consumer.user.name, or @consumer.user.email. Delegation allows you to simplify this:

class User < ActiveRecord::Base
  belongs_to :consumer
end

class Consumer < ActiveRecord::Base
  has_one :user
  delegate :name,:email,:to => :user
end

Now you can refer to @consumer.name and @consumer.email to retrieve and set values for those attributes directly.

This is all well and good if you can be sure that there will always be a user for any given consumer. If consumer’s user is nil and name is called, however, an exception will be raised (because nil.name is undefined). There’s a new feature in Rails 2.2 that relate to delegation and using a prefix for delegated methods also you can set default value by extending delegate method

Delegate with default

class Consumer < ActiveRecord::Base
  has_one :user
  delegate :name,:to => :user, :default => "Sapna Prajapati"
end

Delegate with prefixes

Delegate prefixes just appeared in edge Rails and will work in Rails 2.2. If you delegate behavior from one class to another, you can now specify a prefix that will be used to identify the delegated methods. For example:

class User < ActiveRecord::Base
  belongs_to :consumer
end

class Consumer < ActiveRecord::Base
  has_one :user
  delegate :name,:email,:to => :user ,prefix => true
end

This will produce delegated methods @consumer.user_name and @consumer.user_email.

The problem is if you refactor user or consumer , you’ll have to go back and fix all the places you did this access chaining as well. Means if you rename the user class, you’re almost always going to want to rename that prefix too.But delegates provide custom prefixes so you will end up to change.

Delegate with custom prefixes

class User < ActiveRecord::Base
  belongs_to :consumer
end

class Consumer < ActiveRecord::Base
  has_one :user
  delegate :name,:email,:to => :user ,prefix => :owner
end

This will produce delegated methods @consumer.owner_name and consumer.owner_email.