Ruby On Rails Classroom image

Pooja  Negi / Student / Web Technology

 
To post your Question Join Classroom
 
Lesson Topics's No:-  First|1 | 2|
Lessons:-Modifying Built-in Classes -A Controller Class

 A Controller Class


All this talk about classes and inheritance may have triggered a flash of recognition,  because we have seen both before, in the StaticPages controller (Listing 3.15):

 

class   StaticPagesController  < ApplicationController
def   home
   end
   def   help
end
def  
about
    end
end

 

You’re now in a position to appreciate, at least vaguely, what this code means: StaticPagesController is a class that inherits from ApplicationController and comes equipped with home, help, and about methods. Since each Rails console session loads the local Rails environment, we can even create a controller explicitly and examine its class hierarchy:13  

>>  controller  =  StaticPagesController  .new
=>   #<Static  Pages  Controller:  0x22855d0>
>> controller. class
=> Static  Pages  Controller
>> controller .class .superclass
=> ApplicationController
>> controller.class .superclass .superclass
=> ActionController  ::Base
>>  controller. class .superclass .superclass .superclass
=>  ActionController ::  Metal
>>  controller .class .superclass .superclass .superclass .superclass
=>  AbstractController  :: Base
>>  controller .class .superclass .superclass .superclass .superclass .superclass
=>  Object

 

A diagram of this hierarchy appears in Figure 4.3

 

 

      Figure 4.3 The inheritance hierarchy for the StaticPages controller.

 

We can even call the controller actions inside the console, which are just methods:

>>   controller.home
=>   nil

 

Here the return value is nil because the home action is blank.
        But wait—actions don’t have return values, at least not ones that matter. The point of the home action, as we saw in Chapter 3, is to render a web page, not toreturn a value. And I sure don’t remember ever calling StaticPagesController.new anywhere. What’s going on?

               What’s going on is that Rails is written in Ruby, but Rails isn’t Ruby. Some Rails classes are used like ordinary Ruby objects, but some are just grist for Rails’ magic mill. Rails is sui generis and should be studied and understood separately from Ruby. This is why, if your principal programming interest is writing web applications, I recommend  learning Rails first, then learning Ruby, then looping back to Rails. 

 

 
 
 
image
Pooja   Negi

Skills    Ruby On Rails

Qualifications :- High School - SSN high school, College/University - HNBGU, College/University - SRHU,
Location :-Ranipokhari,Rishikesh,Uttarakhand,India
Description:- Student
Explore
 

  Students (0)