Ruby on Rails - The Language
Now that everything is installed and we can view the “Congratulations, you’re on Rails!” screen. The next step is the language itself, we will create a simple application. To start, we’ll make a simple “Hello Worldâ€? type example. For this demonstration, we won’t even use the database. Rails is a MVC (Model View Controller) framework, which means that all the output will happen in the controllers and the views. So the first thing we’ll need is a controller. Run this from your rails project directory to generate one:
./script/generate controller hello index
This will generate all the files necessary for our example. Try browsing to http://rails/hello. And you should see something like:
Hello#index Find me in app/views/hello/index.rhtml
Now (just like it tells you) open app/views/hello/index.rhtml. This is the View. It contains the text that will be shown at http://rails/hello or http://rails/hello/index. Change it however you want. Or leave it alone. So it should be getting clearer now that URLs in Rails usually follow the format:
hostname/controller/view
Now take a look at app/controllers/hello_controller.rb. This is the Controller. It contains program logic that builds the data for the view. Notice the index function. Any public methods on a controller become actions in Rails.
Try adding the following method just before “end� :
def world @greeting = "hello world!" end
Now browse to: http://rails/hello/world
Template is missing Missing template script/../config/../app/views/hello/world.rhtml
We have a problem. The template is missing. “Template� is a rails word for “View�. And it even tells us the path where it was expecting the template. This path has a lot of ..’s in it, but regardless you can probably see where it’s going. So let’s edit app/views/hello/world.rhtml and add the following text:
<%= @greeting %>
Now refresh that last page. And you will see our “hello world!” text. Notice how the controller created a variable @greeting that carried over into the view. All the instance variables (ones that start with @) from the controller get pulled into the view.
Bookmark This!








I am Filipino Web Developer, focusing on PHP in LAMP framework. As a kid, I spent a lot of my time exploring computers and computer games from Atari to PS, from INTEL 80286 - CoreDuo. I am happily married, with two kids. Currently working in Japan as an IT Engineer.