Before moving on, let’s take a closer look at the class that you have created in this application. Rails has named the class by appending Controller to the name you specified when running the controller generator script (HelloWorld
), and it has made it a descendant of the ApplicationController class:
class SayHelloController < ApplicationController
But what exactly is the ApplicationController class? You may recall that I mentioned that the generate controller
script you ran earlier silently created a file called application_controller.rb (Rails 3) or application.rb (Rails 2) inside the /app/controllers folder. This file is the application controller, and if you open it, you will see that it contains a class called as follows:
ApplicationController < ActionController::Base
So, the SayHelloController class descends from the ApplicationController class that itself descends from the Base class in the ActionController
module. You can prove this by climbing back through the hierarchy and asking each class to display itself. This, incidentally, also gives you the chance to try doing some real Ruby programming in the SayHelloController class.
Just edit the contents of say_hello_controller.rb file to match the following (or copy and paste the code from the sayhello1.rb file in the code archive for this chapter):
sayhello1.rb
class SayHelloController < ApplicationController def showFamily( aClass, msg ) if (aClass != nil) then msg += "<br />#{aClass}" showFamily( aClass.superclass, msg ) else render :text => msg end end def index showFamily( self.class, "Class Hierarchy of self..." ) end end
To see the result, enter this address into your browser (once again, change the port number if necessary):
http://localhost:3000/say_hello
Your web browser should now display the following (in Rails 3):
Class Hierarchy of self... SayHelloController ApplicationController ActionController::Base ActionController::Metal AbstractController::Base Object BasicObject
In Rails 2 it will display the following:
Class Hierarchy of self... SayHelloController ApplicationController ActionController::Base Object
Don’t worry about the actual class ancestry; the internal implementation details of the Rails framework are not of immediate interest. The important thing to understand is that a controller is a real Ruby object that inherits behavior from the ApplicationController class and its ancestors. Any Rails controller class you write or that is autogenerated by running scripts can contain normal Ruby code, just like all the other classes you’ve written in previous chapters. Within a controller, you can use all the usual Ruby classes such as strings and hashes.
But bear in mind that the end result needs to be displayed in a web page. This has certain consequences. For example, instead of putting linefeeds ("
"
) into strings, you should use HTML paragraph (<P>
) or break (<br />
) tags, and it is only permissible to call render
once each time a page is displayed, which explains why I’ve constructed a string in the course of calling the method recursively and then passed this to the render
method right at the end:
def showFamily( aClass, msg ) if (aClass != nil) then msg += "<br />#{aClass}" showFamily( aClass.superclass, msg ) else render :text => msg end end
18.227.102.50