Ruby On Rails Classroom image

Pooja  Negi / Student / Web Technology

 
To post your Question Join Classroom
 
Lesson Topics's No:-  First|1 | 2 | 3 | 4 | 5|
Lessons:- Static Pages with Rails

We see from the class keyword in Listing 3.6 that static_pages_ controller.rb defines a class called StaticPagesController. Classes are simply a convenient way to organize functions (also called methods) like the home and help actions, which are defined using the def keyword. The angle bracket < indicates that StaticPagesController inherits from the Rails class ApplicationController; as we’ll see momentarily, this means that our pages come equipped with a large amount of Rails-specific functionality. (We’ll learn more about both classes and inheritance in Section 4.4.)

       In the case of the StaticPages controller, both its methods are initially empty:

 

  In plain Ruby, these methods would simply do nothing. In Rails, the situation is different. StaticPagesController is a Ruby class, but because it inherits from ApplicationController, the behavior of its methods is specific to Rails. When visiting the URI /static pages/home, Rails looks in the StaticPages controller and executes the code in the home action, and then renders the view (the V in MVC from Section 1.2.6) corresponding to the action. In the present case, the home action is empty, so all visiting /static pages/home does is render the view. So, what does a view look like, and how do we find it?

     If you take another look at the output in Listing 3.4, you might be able to guess the correspondence between actions and views: An action like home has a corresponding view called home.html.erb. We’ll learn in Section 3.3 what the .erb part means; from the .html part you probably won’t be surprised that it basically looks like HTML (Listing 3.7).

 

Both of these views are just placeholders: They have a top-level heading (inside the h1 tag) and a paragraph (p tag) with the full path to the relevant file. We’ll add some (very slightly) dynamic content starting in Section 3.3, but as they stand these views underscore an important point: Rails views can simply contain static HTML. As far as the browser is concerned, the raw HTML files from Section 3.1.1 and the controller/action method of delivering pages are indistinguishable: All the browser ever sees is HTML

In the remainder of this chapter, we’ll add some custom content to the Home and Help pages, then add in the About page we left off in Section 3.1.2. Then we’ll add a very small amount of dynamic content by changing the title on a per-page basis.

Before moving on, if you’re using Git, it’s a good idea to add the files for the StaticPages controller to the repository:

$ git add .
$ git commit -m    "Add a StaticPages controller"
 
 
 
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)