0.1 Introduction

Let us get started!

Welcome to the Ruby on Rails primer.

Let us jump into the Hello World example without further ado.

Example Code:

Output Window

The output is exactly what we asked the index method to render. Try changing the render statement in the example to print something different!

Whenever a Rails app receives a web request, it checks a routing table where URLs are mapped to an appropriate method in a 'controller' class, runs that method and returns the result. In the above example, the URL "http://localhost:3000/index" is mapped to the index method in the HelloWorldController class.

Every controller in Rails inherits from the class ActionController::Base. But don't worry if the concepts in this section doesn't make much sense, things will get clearer as we go along!

Dynamic web applications

In the above example, the web application will render the same boring message everytime you make an HTTP GET request to the index page. Let us add some spice to it:

Example Code:

Output Window

What happened here? Take a look at the URL we used to send the request:

http://localhost:3000/index?name=gary prefontaine&age=10

The portion after the ? lists the parameters of the URL. Here they are name=gary prefontaine and age=10. Multiple parameters are separated by &.

Whenever Rails receives a request, it parses the URL and builds a hash out of it. This hash is made available in the controller action, and is always named params. As you can see, we used the values in the params hash to render the output.

Let us see how the params hash would look in a typical Rails request:

Example Code:

Output Window

A quick exercise

Let us finish the first lesson with an exercise: Build the index method so that it accepts the URL parameter name and responds with a greeting text. If the current time is less than 12pm, the greeting text has to include "Good Morning". Any time later, it has to instead include "Good Evening".

You should also address the person in the greeting message. For example, if the name is "Jack" and the time is before 12pm, it should respond with "Good Morning, Jack!". Anytime later, it should be a "Good Evening, Jack!".

The tests will guide you to reach the correct solution. Remember: if you are stuck at any point in RubyMonk, click the "Need a Hint?" link near the exercise. Failing that, use the Support link at the top to talk to us. We would love to help you out!

Now, good luck with the exercise!

Hint

You need to write a conditional that uses the CurrentTime.in_hours method to check for the time. Depending on the time, pass a string to the render method that includes 'Good Morning'/'Good Evening' alongwith the name parameter.

Output Window

Fantastic! You just wrote a simple Rails controller that accepts dynamic data from URL parameters, applied some logic over it by looking at the current time, and rendered an appropriate greeting message. Not bad for a first lesson.

But before you go to the next one, I want you to take a look at the solution for the above exercise. Click on 'See the solution' link near the exercise and read through the code.

You can see that I built a class called Greeting to encapsulate the greeting logic and used that in the controller. This made the controller a simple single-line method. You too should keep your controllers thin by encapsulating logic in domain objects. By the time you are done with this book, you will learn how to do that and write lean beautiful Rails code!

Congratulations, guest!


% of the book completed

or

This lesson is Copyright © 2011-2014 by Jasim A Basheer