Adding Some Data

As pretty much every piece of Rails documentation will suggest, views are really meant to provide users with a perspective on data managed by a controller. It’s a little strange to run through all this generation and layers of folders just to create an HTML file. To start taking advantage of a little more of Rails’ power, we’ll put some data into the controller for hello, hello_controller.rb, and then incorporate that data into the view.

If you open app/controllers/hello_controller.rb, you’ll see the default code that Rails generated, like that in Example 2-3.

Example 2-3. A very, very basic controller that does nothing

class HelloController < ApplicationController

  def index

This is the first real Ruby code we’ve encountered, so it’s worth explaining a bit. The name of the class, HelloController, was created by the script generator based on the name we gave, Hello. Rails chose this name to indicate the name and type of the class, using its normal convention for controllers. Controllers are defined as Ruby classes, which inherit (<) most of their functionality from the ApplicationController class. (You don’t need to know anything about ApplicationControllers, or even classes—at least not yet—so if you don’t understand at this point, just enjoy the generated code and keep reading.)


If you need to learn more about Ruby to be comfortable proceeding, take a look at Appendix A, “An Incredibly Brief Guide to Ruby.”

def index is the start of the index method, which ...

Get Learning Rails: Live Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.