Step by Step Intro to Ruby on Rails

As you know I’ve been messing around with Ruby on Rails for the last week or so and this will be a step by step intro into the world of Ruby on Rails.

Ruby on Rails has just blown me away, if/when I ever do a private project again this will be the platform I work from. Not only is it easy to learn but your development time so much shorter compared using a language like ASP.Net with C#. Now don’t get me wrong, I am undoubtly a C# fanboy of note but there is something exciting about working with Ruby on Rails. Something exotic.

I am going to do two sets of examples for the code throughout this tutorial, the Ubuntu way and the windows way for those of you that still have not converted. If the windows code does not work please leave a comment and I’ll do my best to help you.

First off, let me explain the basics of Ruby on Rails (RoR). RoR implements a MVC pattern. MVC stands for Model, View, Controller. If you’re new to different programming patterns then check out this wiki link. In a nutshell, this pattern separates your Data and the user interface and all the data moves between these tiers via a controller class as seen below

Model View Controller Diagram

The controller classes handle all web requests sent to the application.

Now before we get started please make sure you have MySql and RoR installed. For the people using Ubuntu check out the following posts I have made regarding the installation of RoR and MySql:
Ruby on Rails in Ubuntu 7.10 (Gutsy)
Setting up MySql 5 in Ubuntu

For people running windows, make sure you have the following installed:
Ruby
      then open your command window and type: gem install rails –remote and answer “y” to any question it may ask.
MySql

once everything has been installed we are ready to begin:

Ubuntu Users:

open a terminal and type in rails appName and obviously replacing appName with whatever you would like to name your app. RoR will then create all the necessary file under your /home folder.

Windows Users:
Open a command window and navigate to where you would like the application placed, then type rails appName

This command will create all the files and folders needed for your new RoR project.

Once you have done this, open up MySql and setup your database. For Ubuntu users I suggest MySql Navigator, just search for it under Add/Remove Programs. Create a new database on your MySql server but do not create any tables just yet. Now that your database is set up, navigate to the config folder in your RoR Project and open the database.yml file. Open it in any text editor. Once its open you’ll see something interesting already. RoR gives you 3 connection options in for your application. You can have a connection to a Test Database, Development Database and a Production database. So instead of having to change connection strings later you can set them up right now. Considering this isn’t much of an issue now since we’re just playing around with RoR, set all connections up with the same settings. enter your database name, MySql UserName and password in this file and save it.

Now that our connections have been configured lets go create that table. The only “strict” part of RoR is the table naming standards. The reason behind this is that RoR implements ActiveRecord. ActiveRecord is an ORM tool. If you’re not familiar with this check out the links that I provided. Although you might initially think that being forced to name tables and columns in certain is not for you, trust me, its worth it later as you’ll see.

Lets create a simple table in the database. Create a table named “users”. Please note that the table name MUST BE lowercase. This is one of those little restrictions. Create a tinyint field and make it an auto-incrementing field. Also specify this field as the primary key of the table. Again, the field name MUST BE lowercase. Then create 2 more fields called name and age. Once this is done the fun can begin.

Remember we spoke about Models and Controllers earlier? Now we are going to create our User model and controller.

Ubuntu Users:
Open a terminal and navigate to your project folder. type “script/generate model User”
once that has completed type “script/generate controller User”

Windows Users:
Open a command window and navigate to your project folder. type “ruby script\generate model User”
once that has completed type “ruby script\generate controller User”

By generating the model and controller, the RoR framework automatically checks the database you specified in the connection file, and from this maps the class you a database table. Remember we named the table “users” yet when we generated the model and controller we created them under “User”. The RoR framework searches the database for the plural of the model name entered. Even if you named your model Company, it would search the database a “companies” table. Thus the RoR framework has a firm understanding of plurals in the english language!

This next step for me is the most magical line of code I have probably ever written. Open the project folder. Navigate to the controller folder under the app folder. Open the user_controller.rb file in a Text Editor.
Here’s what you should see

Now in the class declaration type the following:
scaffold :user

Now as you’ll see in about 2 minutes, this is an amazing line of code. By writing this single line of code you have created the following pages:
1) List Users Page
2) Create new User Page
3) Edit User Page
4) Display User Page
5) Delete User Page

All of these pages are now fully functional. Thats right, you can now start populating your database with information. To test this simply start the RoR webserver and check it out for yourself!

Ubuntu Users:
Open a terminal, navigate to your project folder and type “script/server” BUT do not close the terminal window.

Windows Users:
Open a command window and navigate to your project folder and type “ruby script\server”

Once this has been done click on the following link: http://127.0.0.1:3000/user/list. This is obviously a link to your personal computer so if you’re reading this and haven’t physically followed the steps mentioned above then don’t click on the link as it won’t do anything.

From here you can add new users, view the users, edit and delete them. As you can see the Ruby on Rails Framework is a real development time saver. This is also dependent on your project type. I wouldn’t personally user RoR for an enterprise wide application that was business critical. But for small applications and for proof of concept applications for demo purposes I don’t think there is anything better. Naturally this is a personal opinion.

Posted under Ruby on Rails by StevenMcD on Sunday 11 November 2007 at 8:30 pm

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment