Your First Ruby on Rails Program
Now that you have created your RoR framework we will make a very basic RoR program. For this tutorial we will be using Pico, Putty and Windows.
Windows is obviously an Operating System (OS), this tutorial assumes it is the OS running on the computer you are viewing this page in.
Putty is the SSH program for connecting to the Linux server from Windows using SSH.
Pico is a Linux text editor.
You are going to need a shell account for this to work, so if you do not already have one, please contact support so that we can enable a jailshell for your account.
Login to your jailshell account using Putty (or your favorite SSH program) using your cPanel username and password. Once logged in, navigate to your "test" directory that we created in the previous tutorial. For simplicity, you can copy and paste the commands below into your terminal session.
From the commandline type in "cd public_html/test" then press enter.
Code:
cPanel-user@server [~]# cd public_html/test
You just changed directories (cd) to your "test" directory.
Next execute this command from your Rails application directory ("test") to generate your Rails controller:
Code:
cPanel-user@server [~/public_html/test]# ruby script/generate controller hello index
Now you need to build your program. You can use notepad or some other text editor, and upload the file, or you can edit it right in your shell using pico.
Code:
cPanel-user@server [~/public_html/test]# pico app/controllers/hello_controller.rb
You are now using the Pico text editor.
You will see something similar to:
Code:
class HelloController < ApplicationController
def index
end
end
Change this files so that it appears like the below:
Code:
class HelloController < ApplicationController
def world
@greeting = "hello world!"
end
end
Press ctrl+x and type "y" for yes, then press enter to write to the file.
"def world" is a method called whenever users request yourwebsite.com/hello/world
@greeting is a variable, and is also an object. The @ means it will be seen by the code in your application until the end of the request. We will be using it later in the view.
Finally we need to make the file "world.rhtml" in app/views/hello/world.rhtml by using Pico again:
Code:
cPanel-user@server [~/public_html/test]# pico app/views/hello/world.rhtml
You will be looking at an empty file. Add this line of code to it, then save and exit.
Code:
<%= @greeting %>
Now go back to your cPanel > Ruby on Rails and make sure your application is running. If it is showing as "Not Running" you can click the "Run" button in the "Actions" column for "Available Ruby on Rails Applications."
Once you have started the application, you can view it on the web, here:
http://domain.com/test/public/hello/world/
(domain.com is your actual domain)
If you would like to shorten the URL up a bit, you can symlink it like so:
Code:
cPanel-user@server [~/public_html/test]# cd ../
cPanel-user@server [~/public_html]# ln -s /home/cPanel-user/public_html/test/public new
Now you can access the app at http://domain.com/new/hello/world
If you have any questions or comments, or would like to add to this tutorial, please, feel free! Just hit the "Post Reply' button below.