Archive for the ‘Ruby on Rails’ Category

Step by Step Intro to Ruby on Rails

Posted in Ruby on Rails
Post date: November 11, 2007

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.

Ruby on Rails in Ubuntu 7.10 (Gutsy)

Posted in Ruby on Rails
Post date: November 2, 2007

So I’ve decided after a few months now to give Ruby on Rails a full go after my September debacle with Circle Hosting. Long story short I’ve just set it up in Ubuntu now. As I have previously stated, the more I use Ubuntu the more awesome it becomes.

It seems that ruby comes installed by default in Ubuntu 7.10 which was awesome. I quickly installed RubyGems and updated and installed the supporting documentation and within 2minutes it was ready. I kid you not, 2minutes and it was setup. Obviously I still to install mySql but getting Ruby on Rails installed was literally that quick in Ubuntu 7.10! I decided to just quickly share what I did to install RoR in Ubuntu so quick.

Open a terminal and execute the following commands::

1) sudo apt-get install ruby irb ri rdoc ruby1.8-dev2.2
2) sudo apt-get install rubygems
3) sudo gem update
4) sudo gem install rails –include-dependencies (thats two dashes in front of “include”, Wordpress seems to remove the second dash)

Thats it! 4 easy steps to Ruby on Rails in Ubuntu!

Ruby on Rails talk by Armand

Posted in Ruby on Rails
Post date: August 15, 2007

Just a massive thanks to Armand for an awesome talk tonight. I hadn’t realized just how awesome ruby on rails is. Check out armand’s ruby on rails blog here : http://armandddp.net/

Blogged with Flock

Topic of the Month

Post date: July 16, 2007

I have decided, rather than just blogging about some random topic I might encounter during the day, I’m going to choose  a monthly topic or topics. Idea behind this is to further my research into different areas and focus on that area for approx a month or so. I have already decided next next month I’ll be focusing on Ruby and Ruby on Rails. I have seen Ruby On Rails in action and it seems quite cool.

Also, Armand from dotNet.Org.Za will be presenting a talk at the next SaDev meeting and giving a Ruby on Rails presentation. To RSVP go to either Armand’s blog entry at http://dotnet.org.za/armand/archive/2007/07/13/hitting-the-rails.aspx or register at saDev and goto the thread located at http://sadeveloper.net/forums/thread/6159.aspx

There will also be a “movie evening” the on August 8th regarding Ruby, RSVP at http://sadeveloper.net/forums/thread/6162.aspx