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 end end
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
ApplicationController
s, or even classesâat least
not yetâso if you donât understand at this point, just enjoy the generated
code and keep reading.)
Note
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 ...
Get Learning Rails: Live Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.