Say Hello to Ruby on Rails

I am writing this tutorial with the assumption that you have already installed Ruby on Rails on your computer. Installing Ruby on Rails isn’t exactly the easiest process if you are inexperienced with command-line interfaces, like I am, but I found Lynda’s Essential Ruby on Rails Training to be a huge help.

Before we get started with the Hello World app, you need to install a text editor such as E Text Editor which is available from http://www.e-texteditor.com/ This is just a good tool for editing web pages and great for working with Ruby on Rails. If you’re on a Mac, TextMate is the original inspiration for this program.

Step 1:

First of all you’re going to need to launch Command Prompt from Start > Programs > Accessories > Command Prompt.

Once you have Command Prompt open, navigate to your Ruby on Rails directory (most likely to be C:\Ruby unless you specified otherwise during installation) by typing:

cd c:\ruby

OK, so if your screen command prompt now starts with “C:\Ruby” we can now move on to creating the project.

To create your first project (or any new project for that matter) you need to alter your command line to read:

C:\Ruby rails my_app

This will tell Rails to create a new set of project files as below:

Rails has now created a series of directories and files in your Ruby directory that will be the basis for your application.

Step 2:

The next step is to launch our server. Currently, the web server that ships with Rails is Mongrel.

What we’re going to do now is run a script. You can view the scripts in your project by going to C:\Ruby\my_app\Scripts but as you might guess, the one we are concerned with at the moment is ‘Server’.

To start the server we need to first go to our project directory by typing:

cd my_app

Now we’re in our project folder we can run our server by typing:

ruby script/server

If you see something like the screen adobe, your web server should be up and running. However, just to test this, we need to open our browser and enter:

http://localhost:3000

Hopefully you should see a page like the one below:

If not, just go back and make sure you followed the previous steps correctly.

Step 3:

So, working from the project we generated earlier on in step 1, we are now going to create a controller. The controller is what will be responsible for the actions we request from our application.

To generate a controller, all we need to do is run another script from our scripts directory that we saw earlier. To do this, open another Command Prompt window so as not to interrupt the web server we already have running. Make sure you are in our project folder and alter the command line to read:

C:\Ruby\my_app\ruby script\generate controller Say

If you did this correctly, you should see the following:

What we are doing here is telling RUBY to use the script GENERATE to generate a CONTROLLER called “Say”. As you can see, the script has also created other files, but we’ll worry about those later.

Step 4:

Once we’ve created the controller, we now need to open-up E Text Editor. We can do this from the command line by adding the following line to c:\ruby\my_app in Command Prompt:

e app\controllers\say_controller.rb

You should now see the contents of say_controller.rb in E, and we can go on to edit this to serve some kind of function:

After adding

def hello
end

We can go on to add a VIEW to this controller. A view is exactly what you might think it is. It is the front end of what the user will see and interact with. So what we are going to do now is create a view that we can display in a browser.

The GENERATE script has already created a folder for us within VIEWS, so all we need to do is create the RHTML file. This particular extension is practically the same as HTML, but the ‘R’ at the beginning of the extension implies that the file will include some elements of Ruby on Rails.

The extension .rb as you might guess belongs to Ruby. It implies that the content of the file is PURE Ruby, whereas files such as RHTML only include SOME Ruby.

Click the new file button in the lower-right of your E window and name the file hello.rhtml

We can now edit this view file as we would a normal HTML file. Enter the following (or something similar) into your currently blank hello.rhtml file:

<html>

                <head>

                                <title>Hello World</title>

                </head>

                <body>

                                <p>Hello World!</p>

                </body>

</html>

…and save it.

Now, before we try and view it in our browser we need to restart Mongrel (our web server) by returning to the first Command Prompt window that is running the server and pressing Ctrl+C and then re-launching the server by entering:

ruby script\server

This is necessary in this case to let us view the changes we have made.

Now we can open our browser and go to the address:

http://localhost:3000/say/hello

You should be seeing ‘Hello world!’ in your browser window.

Conclusion

This was a starting block on your way to learning Ruby on Rails and up to now you have learnt how to make a static web page. Of course, RoR is not just for making static web pages, but web applications. This was just a very basic introduction to the framework.

Advertisements

1 Comment »

  1. Very good info!

    I’ve bookmarked this site!

    Charles


{ RSS feed for comments on this post} · { TrackBack URI }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: