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:
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
 |