Ruby On Rails

=About= Ruby on Rails is a web application framework using the Ruby programming language. Rails provides users an easy way to develop complex, dynamic web apps using the Model-View-Controller (MVC) design pattern and integrate a number of common web application features. Rails provides a great deal of flexibility for configuration, and can be configured to run with other technologies such as XAMPP stack (Apache and MySQL). Generation of objects such as models, controllers and views can be easily done using the Rails command interface

=Installation= The first step is to download the Rails installer. The file can be found here. Accept the terms, and choose the installation location for Ruby on Rails. If you are planning on configuring Ruby to run using Apache and MySQL via XAMPP, it is best to create the following directory structure C:\xampp\ruby\. Ruby will be installed inside XAMPP. Otherwise, it would make most sense to install at C:\Ruby. Keep both boxes checked, and install. Ruby on Rails has been installed.

=Configure With XAMPP= One benefit of Rails is that it can be configured to run with XAMPP. To do this, we must build a directory structure for it in the Apache configuration file, and listen to port 3000 (not port 80 or 443 like we would expect with PHP applications).

Configure With Apache
First, open C:\xampp\apache\conf\httpd.conf in your XAMPP install directory. At the top of the configuration, find the following line:

LoadModule rewrite_module modules/mod_rewrite.so

Make sure that this line is not commented out (lines are commented out if they start with a # character). Next, we must create Virtual Host for our Ruby applications. Scroll down anywhere in the directory listings section, just below the LoadModule section. Add the following line of code:

Listen 3000

This line will tell Apache to listen to port 3000 as well as 80 and 443. After this line, add the following:

 ServerName rails DocumentRoot "c:/devel/web/ruby"  Options ExecCGI FollowSymLinks AllowOverride all Allow from all Order allow,deny AddHandler cgi-script .cgi AddHandler fastcgi-script .fcgi  

The following will create a Virtual Host running on port 3000, with a document root specified by the user. We then create a directory, pointing to our document root with the standard directory configurations. Now we should test our installation and Apache configurations. Open a command line process, and change directories into C:\devel\web\ruby, create the directory if it does not already exist. Once inside the directory, run the following command:

rails new test

Rails will proceed to create a number of important files for the project. Change directories into your new project folder, which in this example we called test. Start the Apache server using the XAMPP control panel. You should see three ports that Apache is listening to.



Now open a browser. Point the browser to the following URL: http://localhost:3000/test/public. If you analyze the files and directories Rails created when generating the project, you will see that there are html files inside the public directory. There should be a file called index.html in here, if not create it. In the browser we should now see a welcome page for Ruby on Rails. Rails has now been configured with Apache. You no longer need to run the "rails server" command to start rails, as this will create an error because Apache is already listening to port 3000.

Configure With MySQL
The next step in configuring Rails with XAMPP is to change the database to MySQL. This process is somewhat more complicated. The first thing to do is to modify the database settings for the Ruby project. These settings are available in the file config/database.yml inside your Ruby project. When the file opens, you will see three separate processes to be configured, development, test and production. All three will need to be changed.


 * Note, there is a shortcut to setting MySQL as the database. When creating the project, use the following command instead:

rails new test -d mysql

If you use the above command, you will see MySQL2 as the adapter for all three processes. Otherwise you will see a different database engine, sqlite3. In either case, we must change the database to mysql (mysql2 is a faster variant of the mysql adapter, but there are problems with using it on Windows. There is a problem loading the mysql2 gem). With the adapter changed, next we must specify a gem to use.

In the base directory for your project, you will see the GemFile file. This file contains all your gems (for more details on gems, see the "Creating a New Rails Project" section below). Inside the file, replace the "gem 'sqlite3' line with the following:

gem 'mysql'

At the command line (you must be inside your project directory), update your gems using the following command:

bundle install

Ruby on Rails is now configured to run using a MySQL database. There is one serious problem though. The mysql gem we installed expects that we will be using mysql version 6.0.0. This may not be the version of MySQL we have installed. We have two options here, we can update MySQL to version 6.0.0 or we can use a MySQL connector to bridge the gap. For flexibility, we will do the latter. MySQL connectors are special drivers used to bridge the gaps between SQL versions and other applications interfacing with MySQL. Using a connector we can fool Rails into believing we are using MySQL 6.0.0.

First, download the proper connector file from the MySQL website, found here. Download the 32 bit .zip file version. When the file has been downloaded, extract the contents into C:\xampp\ruby\sql or any other directory you will be able to locate. At the command line (once again inside your project folder), run the following command:

gem install mysql -- -- --with-mysql-dir=C:/xampp/ruby/sql/mysql-connector-c-noinstall-6.0.2-2in32

The mysql gem will be re-installed taking into account the connector we just downloaded. The last step is to find the libmysql.dll file inside the lib directory in our mysql connector directory we just extracted. Copy the file into C:\xampp\ruby\RailsInstaller\Ruby1.9.3\bin. Rails has now been configured to use MySQL as its database, and will connect with any version you have installed, because of the connector.

=Programming in Ruby Using Rails=

Creating a New Rails Project
When Rails has been installed, the first thing to do is create a new project. Creating a new project in Rails is simple. At the command line interface, change directories into the directory you want to create the project in. If you are using Apache as the web server, it is crucial that you create the project inside the root directory specified in httpd.conf. When you are inside the correct directory, run the following command:

rails new c:\devel\web\ruby\project_name

This will create a directory for your Ruby project and will create all the needed files for Rails. There are a few important files to note. The first important file is GemFile. GemFile will control the gems used by Ruby. In Ruby, gems refer to the packages which will be loaded in the Ruby project.

We can add gems in a number of ways. The first is to add the gems into GemFile, following the format:

gem 'gem_name', 'parameters'

The other way to add gems is to use the gems command at the command line. This follows the same format as above, with the possibility of adding options as well. Whenever we make changes to GemFile, it is important that we update our gems. We can do this using the following command:

bundle install

This will install the gem and all its dependencies. We can also update the gem files as needed by running the following command at the command line:

bundle update

Another important file to note is config/database.yml. This file specifies the details of our database (host, username, password and database name). If we are using a MySQL database we must change some of the values for our new database. As mentioned, we can alternatively specify MySQL as the database when creating the project by running this command instead:

rails new c:\devel\web\ruby\project_name -d mysql

Just remember to change the adapter type from mysql2 to mysql if you are having trouble installing the mysql2 gem.

Ruby Guide
Ruby is an object-oriented, open source programming language. It was designed with simplicity in mind, and follows a series of rigid design/implementation patterns that hold true consistently throughout the language. For instance, in Ruby everything is an object. This ideology is held consistently, even more-so than other programming languages such as Java which maintain a level of separation between objects and simple datatypes. Because of this, in Ruby datatypes such as integers are objects. Further OO principles are explored in Ruby through its explicit scoping and methods. Unlike other languages, all instance variables in Ruby are private to that class. This forces users to maintain OO design and use getters/setters to access class data outside of the class.

Data
Data in Ruby can represented in a number of formats. To access data, much like in other languages we can store data via variables. variables follows a number of formats depending on the scope of the variable. Variables are typeless in Ruby.

Classes and methods
In Ruby, a method declaration works as follows:

def &#60;NAME&#62;(&#60;PARAMETERS&#62;...) #Content goes here end

Generating the MVC
To develop a web application using Ruby on Rails, first you must generate the resources needed to satisfy the MVC (Model View Controller). MVC is a design pattern used to describe the relationships between core software functionality, which can be expressed as one of three components. These components are the model view and controller. Each component serves a primary function and interfaces with other components.