Archive for the ‘advanced-rails’ Category

Using nginx + mongrel + rails on Ubuntu

Friday, October 5th, 2007

STEP 1
Install Nginx. Get the source code from here. Before you compile make sure you have SSL libraries if you plan on using the SSL mode. To get the ssl libraries run:

sudo aptitude install openssl libssl-dev

To compile:


./configure --with-http_ssl_module
make
sudo make install

To start nginx run


sudo /usr/local/nginx/sbin/nginx

STEP 2
Edit

/usr/local/nginx/conf/nginx.conf

file to setup configuration for your rails app and mongrel cluster. Here is a sample of nginx.conf file:


worker_processes  1; 

error_log  logs/error.log;
error_log  logs/error.log  notice; 

pid        logs/nginx.pid; 

events {
    worker_connections  1024;
} 

http
{
        include       conf/mime.types;
        default_type  application/octet-stream;
        server_names_hash_bucket_size 128; 

        sendfile        on;
        tcp_nopush     on; 

        keepalive_timeout  65;
        tcp_nodelay        on; 

        upstream mongrel_test {
            server 127.0.0.1:8000;
            server 127.0.0.1:8001;
            server 127.0.0.1:8002;
        } 

        gzip on;
        gzip_min_length  1100;
        gzip_buffers     4 8k;
        gzip_types       text/plain; 

        # ************************ server conf begins **********************
        server
        {
                listen       80;
             server_name  servername.com;
             root    /home/shovan/apps/oas/public; 

             access_log  off;
             rewrite_log on; 

            # this rewrites all the requests to the maintenance.html
            # page if it exists in the doc root. This is for capistrano's
            # disable web task
            if (-f $document_root/system/maintenance.html) {
              rewrite  ^(.*)$  /system/maintenance.html last;
              break;
            } 

                location ~ ^/$ {
                  if (-f /index.html){
                    rewrite (.*) /index.html last;
                  }
                   proxy_pass  http://mongrel_test;
                } 

                location / {
                  if (!-f $request_filename.html) {
                    proxy_pass  http://mongrel_test;
                  }
                  rewrite (.*) $1.html last;
                } 

                location ~ .html {
                                root    /home/shovan/apps/oas/public;
                } 

                   location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js|mov)$ {
                  root /home/shovan/apps/oas/public;
                } 

                location / {
                    proxy_pass  http://mongrel_test;
                    proxy_redirect     off;
                    proxy_set_header   Host             $host;
                    proxy_set_header   X-Real-IP        $remote_addr;
                    proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
                } 

        }

} # end of http

STEP 3

Set up your mongrel_cluster.yml file. Here is how it should look like:

 ---
cwd: /home/shovan/apps/oas
port: "8000"
environment: production
address: 127.0.0.1
pid_file: log/mongrel.pid
servers: 3

STEP 4
Now start the mongrel cluster using the following command:

 sudo mongrel_rails cluster::start

That should fire up the clusters. Restart ngnix and you should be able to see your rails app when you load up the url in the browser. If you get errors make sure you look at the logs to see whats going on. They can be really helpful.

Installing Ruby bindings for SVN on Ubuntu

Saturday, July 21st, 2007

I just spent four painful hours :( trying to setup Collaboa on my Ubuntu box. I hope to share my findings hoping it will save you guys some time.

Collaboa is a collaborative tool for developers using Subversion. It is written in rails and lets you do repository browsing, track issues, manage milestone, and view changes.

First thing you need to get is the ruby bindings for subversion. Please do not try version 1.3.25. It just won’t work.

STEP 1
You can download the ruby bindings for subversion here .

tar xzvf swig-1.3.31.tar.gzcd ./swig-1.3.31./configuremakesudo make install 

STEP 2
Download and install the latest version of Subversion

wget http://subversion.tigris.org/downloads/subversion-1.4.4.tar.gztar xzvf subversion-1.4.4.tar.gzcd subversion-1.4.4./configure –prefix=/usr/local –with-openssl –with-ssl –with-zlib make sudo make install 

STEP 3
Now, install the Ruby subversion bindings

make swig-rb
sudo make install-swig-rb
sudo apt-get install libsvn-ruby

Then start irb and do a ‘require “svn/core” to find out if u were able to install swig-rb properly.

Validations based on a Controller Method

Thursday, June 21st, 2007

Sometimes i have ran into a situation where there was a need to validate a new record depending upon which method was used. For example I was capturing data for an event which had both domestic and international leads. There were different criteria for the two sets of data.

For eg: I needed to validate presence of state, cost center for US leads and ignore those for international leads.

So i needed to figure out separate ways to handle the validations. Here is how I implemented it.

I defined two methods in my model: usa_validation? and international_validation?


  # validations only specific to usa leads
  attr_accessor :usa_validation

  def usa_validation?
    return usa_validation
  end

  def usa_validation=(value)
    @usa_validation = value
  end

  validates_presence_of :region, :if => :usa_validation?
  validates_presence_of :cost_center, :if => :usa_validation?

  # validations only specific to international leads
  attr_accessor :international_validation

  def international_validation?
    return international_validation
  end

  def usa_validation=(value)
    @usa_validation = value
  end

  validates_presence_of :country, :if => :international_validation?

In my controller i set the flag to true for US or international depending upon the case.


  def capture_usa_lead
    @lead = Lead.new(params[:lead])
    @lead.usa_validation =  true
    if @lead.save
      #display message
    else
      render  :layout => “lead”, :partial => “usa_form”
    end
  end
  def capture_international_lead
    @lead = Lead.new(params[:lead])
    @lead.international_validation = true
    if @lead.save
            #display message
    else
      render  :layout => “lead”, :partial => “international_form”
    end
  end