This is intended to be a brief introduction to developing Ruby on Rails applications. At the bottom of this article you will find a number of resources to help you learn more about Ruby on Rails and related information, as well as links to some rails tutorials that will go into more depth than this document.
The Model-View-Controller (abbreviated to MVC) design pattern is fairly straight-forward, it simply means that your program is split into three separate components: The Model, View, and Controller. The “Model” is your data, no matter how it’s stored. If you’re writing a blog, this is where all of your posts and comments would go. The “View” is your interface. In the case of ruby on rails, we’re talking about the part that displays your HTML. The controller handles the business logic, and ties the model to the view. MVC programming is beneficial for many reasons.
From this point on it is assumed that you have an understanding of both object-oriented design and MVC, and now you can get into how to develop rails applications. A few additional notes before you start:
First of all, you need to have SSH access enabled. Contact Support if you have trouble with this.
Secondly, you’ll see a lot of tutorials referring to a program called “script/server” or “webrick”. This is NOT NECESSARY on when using cPanel, and you should never have to use it. This is designed for people who are developing their rails application on their own computer where there is no apache install which is pre-configured to use Ruby on Rails. However, you do have access with cPanel.
This tutorial is loosely based on the excellent official ruby on rails tutorial which is located at http://wiki.rubyonrails.com/rails/pages/Tutorial although this document has been adapted to fit some cPanel-specific things.
To begin, log into the server using SSH. You’ll need a work area for your rails application. Assuming ahead of time that you may eventually want multiple applications, you should make a work directory and then cd into it. You can name it whatever you would like, but this document assumes that it is called “rails”.
% mkdir ~/rails
% cd ~/rails
Now you may create your application. As we are just making a simple Hello World application, we’ll assume that the application is named “first”.
% rails first
% cd first
Next, we’re going to set up a subdomain for this application to run on. Log into your cPanel, click on ‘subdomains’, then type ‘first’ into the first text box and click ‘Add’. You’ve now created a new subdomain, first.yourdomain.co.uk, which will be the new home of your Ruby on Rails application. Now, we’re going to make your application’s “public” directory be the rootdir of that subdomain with the following commands:
% cd ~/public_html/
% rm -r first
% ln -s /home/YOUR_USERNAME/rails/first/public first
You should now be able to go to http://first.yourdomain.co.uk/, where you will see the Ruby on Rails welcome message. As the welcome page suggests, it is now time to set up your databases.
In cPanel, click on ‘MySQL Databases’. The first thing you’ll want to do here is add an SQL user for rails to use. You can name this whatever you’d like. We will assume you used ‘rails’. cPanel prepends your username to the user name, so you should take note of the actual name created (it should be username_rails).
Next, we’re adding a database. Name this database ‘first’, to match your application name. You will again notice that username_ has been prepended. Finally, we’re going to link this username to the database. Select username_rails and username_first from the dropdowns and make sure the ‘All’ checkbox below them is checked, then click the ‘Add User To DB’ button.
Now you should repeat this step, with ‘firstdev’ as the database name, and linking username_rails to it.
Now we’re going to edit the database.yml file. Open ~/rails/first/config/database.yml in your favorite editor, and modify the ‘development’ and ‘production’ sections to contain the username, password, and database that you just created.
Next you should create the actual database data. From the mySQL page in cPanel, you should find a ‘phpmyadmin’ link. Within phpmyadmin, select the “_first” database from the dropdown on the left, then click on the “SQL” tab along the top. Paste the following into your box and click ‘Go':
CREATE TABLE `people` (
`id` int(10) unsigned NOT NULL auto_increment,
`name` varchar(50) NOT NULL default ”,
`street1` varchar(70) NOT NULL default ”,
`street2` varchar(70) NOT NULL default ”,
`city` varchar(70) NOT NULL default ”,
`state` char(2) NOT NULL default ”,
`zip` varchar(10) NOT NULL default ”,
PRIMARY KEY (`id`),
KEY `name` (`name`)
) TYPE=MyISAM AUTO_INCREMENT=2 ;
Now, click on the SQL tab one more time, and run this query:
INSERT INTO `people` VALUES (1, ‘Superman’, ‘123 Somewhere’, ”, ‘Smallville’, ‘KS’, ‘123456’);
Now repeat these two sql queries for your _firstdev database.
The next step is to create a controller.
% ./script/generate controller First list view new edit
After that has been created, you will create your model.
% ./script/generate model Person
Now we’re going to modify two files. First, open app/views/first/view.rhtml and make it look like this:
This page will display one friend
<%= @person.name %>
<%= @person.street1 %>
<%= @person.street2 %>
<%= @person.city %>
<%= @person.state %>
<%= @person.zip %>
Next, open app/controllers/first_controller.rb and modify the ‘view’ method to look like this:
@person = Person.find(1)
Congratulations, You now have a working Ruby on Rails application which reads information from a database. Go toand you should see Superman’s information.
You should now go on to read other Ruby on Rails tutorials. You can find a lot of helpful information at http://wiki.rubyonrails.org/, as well as at http://rubyonrails.org/docs. You should also watch the Ruby On Rails Screencasts, which show you, among other things, how an experienced ruby on rails developer can create a fully functional application in a matter of minutes using ruby on Rails.
More information on MVC: http://wiki.rubyonrails.com/rails/pages/UnderstandingMVC
Official Ruby On Rails Screencasts. You should watch these: http://media.rubyonrails.org/screencasts
Ruby on Rails wiki. The tutorials listed here are quite helpful: http://wiki.rubyonrails.org/
Why’s poignant guide to ruby: You will either enjoy this or hate it, but it’s a nice intro to the ruby language: http://poignantguide.net/ruby