Home Page | Starter Packages | Business Packages | Rent a Server | Web Templates | About Us | Contact  
 

Our Network & Servers
Frequently Asked Q's
Start a New Order
Register a Domain
Latest News
Our Forum (new)
Counter Strike Stats
Technical Support








Can't remember?

Our Live Support is:

Live Help

Secure Payments

You may pay for our services via PayPal.
Find out more ...

Mi smo PayPal verifikovan biznis i to od Maja 2004-e godine!
Since May 24, 2004.

Make a payment with any of the major credit cards!
 

Ruby on Rails - A simple How-To Guide

There are two ways you can do everything in life: easy way or hard way.

Easy way, when it comes to making your website with Ruby, is to have a Shell Access. Shell is just another term for User Interface, which makes interaction with the operating system easier. We offer Shell Access to all of our clients for a small fee, which in turn allows you to access your account from within the shell and do things as if you were sitting right next to our server. (Contact Tech Support if you need Shell Access)

Hard way, would be to not make a use of shell access and keep on going back and forth, uploading your application back to the server when ever a little change happens. You would also have to install Ruby, Rails and MySQL server on your own computer in order to develop applications there. Our tutorial will not cover this scenario since there are many tutorials explaining this on-line.

Note: EVERYTHING you see below is cAsE SeNsItIvE! This is not same as this, or thiS!
Welcome to the Linux World!

The Easy Way
 


1)
This step is not really needed, but it is most likely that you will use a database in your application, therefore we show you how to do it. Easy way to do this is by using our Control Panel. Login to the cPanel on your site by going to www.yourSite.com/cpanel. Use your login credentials sent to you in your welcome email.

Once there do the following:

1 Click on MySQL Databases option.
2 Scroll down the page (if you have to) and in the field:

Db:

Type in ourDatabase and click on
 
3 When prompted of successfully database creation click Go Back link.  
4

Now you will notice that there is a new database created. Next step is creating a user account which can access our new database.

Scroll down again and fill in the following fields like so:

UserName:
Password: (you will see * instead of text)

Then click on
 
5 When prompted of successfully user creation click Go Back link.  
6 Almost there, hang on. We now have a database and a user, next logical thing is to assign that user to the database. To do so, make sure you set your settings as below:

User:

Db:

Privileges: ALL ALTER CREATE TEMPORARY TABLES CREATE DELETE DROP SELECT INSERT UPDATE REFERENCES INDEX LOCK TABLES

Of course, instead of yourUsername you will see your real username, but this is just for our little demo. Make sure that under privileges ALL is checked, and click on

 
7

Congratulations, you will now see a success message informing you that a user has been added to the database. Phew, that was a lot of work.

Thank God we are now going to the easy stuff - working with Rails! Believe it or not, it takes less time to setup a working instance of a demo Rails application that to create this database!

 

Now that we have a database, it would be nice to have a table of our data as well. Let's create contacts table, a small little place where we are going to keep track of our friends. To do so, you may click on the phpMyAdmin link at the bottom of the MySQL section of cPanel, then on the left side menu choose newly created database (yourUsername_ourDbase). Click on the SQL tab in the following page, and in the Text-Box provided enter the following:

 

CREATE TABLE contacts (
id int(10) unsigned NOT NULL auto_increment,
name varchar(50) NOT NULL default '',
PRIMARY KEY (id)
) ENGINE=InnoDB ;

INSERT INTO contacts VALUES (1,'Jane Doe');
INSERT INTO contacts VALUES (2,'John Doe');

 


Click on GO button, and new table contacts with the fields we specified will be created in our database.

2) Log in to your account by using SSH (login details are provided to you in ssh welcome email)

3) Make sure you are in your home directory by typing the following commands:

  serverName$ cd ~
serverName$ pwd

/home/yourUsername

serverName$
 

First command (cd~) ensures that you are in your home directory, and second command (pwd) shows you in which directory you are at the moment.

Note: ~ is a short hand for /home/yourUsername

If your username was john, then you would see /home/john

4) Now that we are sure we are in our home directory lets explain something. Rails applications should NEVER be in your public_html directory for security reasons. So, the best practice is to create a special directory in your home directory where all of your Rails applications will reside. Let's do that now by calling that new directory railApps and by moving into it.

  serverName$ mkdir railApps
serverName$ cd railApps
serverName$ pwd

/home/yourUsername/railApps
 

5) The (pwd) command was not necessary, we used it here just so you can see that you are now working in railApps directory.

6) Since we are doing this the Easy Way, PeconiHosting already has Ruby and Rails installed so all left for you to do is start creating your application. You do that by typing the following command:

  serverName$ rails ourFirstApp  

7) At this moment you will see a bunch of stuff on your screen. It's Rails doing it's thing. It has just created a directory structure for your application.

8) Lets get into ourFirstApp directory and update some permissions, so things can work as they should:

  serverName$ cd ourFirstApp
serverName$ chmod -R 775 log
serverName$ chmod -R 775 public
 

9) Rails is a very friendly fellow: it has only one configuration file you have to edit to make it work: database.yml in the config directory. So, let's do it:

  serverName$ cd config
serverName$ pico database.yml
 


I really like working in pico, it is a nice and simple editor. If you like any other (such as vi), be my guest and use it. Which ever editor you use, make sure that your database.yml file looks exactly like this:

 

development:
  adapter: mysql
  database: yourUsername_ourDatabase
  host: localhost
  username: yourUsername_dbUser
  password: ourSecret

test:
  adapter: mysql
  database:
  host: localhost
  username:
  password:

production:
  adapter: mysql
  database:
  host: localhost
  username:
  password:

 

Once you are done save the file. In pico this press CTRL-X and then confirm changes by pressing the Y key, followed by Enter.

NOTE: To avoid any errors, make sure that you appropriately indent everything under development, test and production! Use 2 spaces (the de facto Ruby standard). Also, we just set the development environment, others are out of the scope of this tutorial.

Great! We are almost there. Lets just make a short summary of what we have right now:

 

We created a database ourDatabase to be used with our first application, and we assigned it a username who can access it with ALL (full) privileges.

We created our first Rails application called ourFirstApp in directory ~/railApps/ourFirstApp.

We also set the appropriate permissions on two directories (log and public) so everything works like a charm later on.

We configured the main database file in our application database.yml

What is left to do is Let Rails do Something Cool!

 

10) Lets make sure you are in your main application directory (~/railApps/ourFirstApp):

  serverName$ cd ~/railApps/ourFirstApp
serverName$ pwd

/home/yourUsername/railApps/ourFirstApp
 

Since we are there, we may start the Rails Magic. Remember that rails is all about less coding, less redundancy, and faster web development? Let's prove it:

  serverName$ script/generate model contact  

Rails will again respond by letting you know it had created some stuff:

  exists app/models/
exists test/unit/
exists test/fixtures/
create app/models/contact.rb
create test/unit/contact_test.rb
create test/fixtures/contacts.yml
 

And now, (drums sound), type the following:

  serverName$ script/generate scaffold contact  

Rails writes out some more things, informing you on what it has created. Without much coding, a nice little application has already been made for us, complete with forms for viewing, editing and deleting your contacts.

11) Just before we go on and test this application we do have to change just one more thing. Rails, out of the "box", runs applications in native cgi mode which is really slow. PeconiHosting supports fast-cgi and you should definitely use it! Do the following:

  serverName$ cd ~/railApps/ourFirstApp/public
serverName$ pico .htaccess
 

You should edit this .htaccess file in public directory of ourFirstApp and change change some lines to match the following:

 

Uncomment line:
    # RewriteEngine On

Make it look like:
    RewriteEngine On

Also change the line:
    # RewriteBase /myrailsapp

To look like:
    RewriteBase /ourFirstApp

And to make everything runs faster change the line:
    RewriteRule ^(.*)$ dispatch.cgi [QSA,L]

To look like:
    RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]

 


Save the file.

12) Our part of creating a small Rails Application is done! We now just have to make this Rails App available on line. If you recall, we did not put it in our public_html directory, therefore it is not accessible from the web. Let's take care of that:

  serverName$ cd ~/public_html
serverName$ ln -s ~/railApps/ourFirstApp/public     ourFirstApp
 

So, what just happened? We went to our public_html directory, one that yourDomainName.com points to, and then we created a symbolic link to our Rails Application's public directory. Why? Well, because, that is what you should do in order for your application to be accessible on-line :)

Now, you have to read a 137 page manual and get your app going ... Just kidding. Go to www.yourDomainName.com/ourFirstApp/contacts and have fun.

If you skip step 11, some times you will have a Server 500 error. Apache describes it as: Premature end of script headers: /home/yourUsername/public_html/ourFirstApp/dispatch.cgi. Doing what was mentioned in step 11 will fix your problem.

This should get you started. If you need more info, help, start at www.RubyOnRails.com.

Tutorial by,

Petar Smilajkov
PeconiHosting Owner/Founder