Home | | Internet & World Wide Web HOW TO PROGRAM | | Internet Programming | | Web Programming | ActionController and ActionView - Ruby on Rails

Chapter: Internet & World Wide Web HOW TO PROGRAM - Rich Internet Application Server Technologies - Ruby on Rails

| Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail |

ActionController and ActionView - Ruby on Rails

Ruby on Rails has two classes that work together to process a client request and render a view. These classes are ActionController and ActionView.

ActionController and ActionView

 

Ruby on Rails has two classes that work together to process a client request and render a view. These classes are ActionController and ActionView.

 

Rails Controller

 

To generate a controller in Rails, you can use the built-in Controller generator. To do that, open the Ruby Console window and navigate to the application directory by typing in:

 

cd pathToInstantRails\rails_apps\applicationName

 

To generate a controller for the welcome application type:

 

ruby script/generate controller Welcome

 

This creates several files including welcome_controller.rb, which contains a class named WelcomeController. Figure 24.12 shows a controller for our Welcome example contain-ing only one method.

 

Line 3 defines a class WelcomeController that inherits from ApplicationCon-troller. ApplicationController inherits from ActionController::Base, which provides all of the default functionality for a controller. The method in lines 5–7 renders text in XHTML format to the browser using the render method

 

Line 6 specifies the text parameter of the render variable using a ruby symbol. Sym-bols are identifiers preceded by a colon that have a particular value or variable associated with them. When specifying a parameter in a method the notation is as follows:

 

parameter_symbol  => parameter_value

 

1    # Fig. 24.12: app/controllers/welcome_controller.rb

 

2    # Simple controller that renders a message on a web page.

 

3    class WelcomeController < ApplicationController

 

4          # render text in page

 

5          def index

 

6                 render :text => "Welcome to Ruby on Rails!"

 

7          end # method index

 

end # class WelcomeController



Fig. 24.12 | Simple controller that renders a message on the web page

Running Ruby on Rails

 

A Ruby on Rails application must be run from a web server. In addition to Apache, Instant Rails comes with a built-in web server named Mongrel, which is easy to use to test Rails applications on the local machine. You can start the Mongrel server through Instant Rails by going to the Rails Application window, selecting the Welcome application from the list and clicking the Start with Mongrel button (Fig. 24.13).

 

One important feature of Rails is its URL mapping ability. Rails automatically sets up your web application in a tree structure, where the controller’s name in lowercase is the directory, and the method name is the subdirectory. Since the controller name is Welcome and the method name is index, the URL to display the text in Figure 24.12 is  http:// localhost:3000/welcome/index. Notice in the screen capture of Figure 24.12 that the URL is simply  http://localhost:3000/welcome. The default action called on any controller is the one specified by the method index. So, you do not need to explicitly invoke the index in the URL to render the text in line 6.


Rendering a View

When generating output, a controller usually renders a template—an XHTML document with embedded Ruby that has the .rhtml filename extension. [Note: The next version of Rails will use the extension .html.erb rather than .rhtml.] The embedded Ruby comes from a library named erb. A method in a controller will automatically render a view with the same name by default. For example, an empty hello method would look for a hel-lo.rhtml view to render. Fig. 24.14 is the Welcome controller with a hello method added. The index method has been removed for simplicity.

 

The server_software method (line 6) is called on the request object—an object that contains all of the environment variables and other information for that web page. The method server_software returns the name of the server that is running the web applica-tion. This name is stored in an instance variable that will be used by the view. Our hello method looks for a hello.rhtml file in the web application’s app/views/welcome direc-tory. Figure 24.15 shows a sample hello.rhtml file.

 

The view consists mostly of XHTML. The erb is shown in line 14, surrounded by <%= and %> tags. Everything between these tags is parsed as Ruby code and formatted as text. Ruby delimiters without an equals sign—<% %>—represents statements to execute as Ruby code but not formatted as text. The @server_name variable is passed in directly from the controller in the view. To run this application, modify the welcome.rb controller file to look like Figure 24.14. Then go to the /app/views/welcome directory, create the hello.rhtml file in Fig. 24.15. Run the welcome application on the Mongrel server (if it is not already running) and direct your browser to the URL  http://localhost:3000/

 welcome/hello.

1    # Fig. 24.14: app/controllers/welcome_controller.rb

 

2    # Simple controller that passes a parameter to the view.

 

3    class WelcomeController < ApplicationController

 

4          # set server_name to server information

 

5          def hello

 

6                 @server_name = request.server_software # retrieve software of server

 

7          end # method hello

 

8    end # class WelcomeController

 

Fig. 24.14 | Simple controller that passes a parameter to the view.

 

1    <?xml version = "1.0" encoding = "utf-8"?>

 

2    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"

 

3          "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

4              

5    <!-- Fig. 24.15: app/views/welcome/hello.rhtml -->

 

6    <!-- View that displays the server name. -->

 

7    <html xmlns = "http://www.w3.org/1999/xhtml">

 

8    <head>

 

9          <title>hello</title>

 

10   </head>

 

11   <body style = "background-color: lightyellow">

 

12         <strong>Hello from the view!</strong><br />

 

13         The server you are coming from is

 

14         <em><%= @server_name %></em>

 

15   </body>

 

</html>


Using a Layout

Often, information spans multiple web pages that could be viewed as a header or footer. Rails allows you to add headers and footers with a layout—a master view that is displayed by every method in a controller. A layout can refer to a template of the method that is be-ing called, using yield. A layout has the same name as the controller, and is placed in the app/views/layouts directory. Figure 24.16 is a layout for the Welcome controller. To add a layout to the application create a welcome.rhtml file in the apps/views/layouts directory. To run this application, re-load the page from Fig. 24.15.

 

Line 9 invokes the action_name method on the controller object. This displays the name of the method that is currently being called in the controller. Instance variables defined in the controller are copied to both the layout and the view that the layout renders. Line 14 is a placeholder for the view content (hello.rhtml in this example) that is specific to the action called in the controller.

 

1    <?xml version = "1.0" encoding = "utf-8"?>

 

2    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"

 

3          "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

4              

5    <!-- Fig. 24.16: app/views/layouts/welcome.rhtml -->

 

6    <!-- Layout that displays a greeting. -->

 

7    <html xmlns = "http://www.w3.org/1999/xhtml">

 

8    <head>

 

9            <title><%= controller.action_name %></title>

 

10   </head>

 

11   <body style = "background-color: lightyellow">

 

12         Hello from the Layout!<br />

 

13         <hr />

 

14         <%= yield %> <!-- render template -->

 

15   </body>

 

</html>



Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail


Copyright © 2018-2020 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.