Writing a Simple Rails Application to Show System Status

Problem

You would like to get started with Rails by building a very simple application.

Solution

This example displays the running processes on a Unix system. If you e developing on Windows, you can substitute some other command (such as the output of a dir) or just have your application print a static message.

First, make sure you have the rails gem installed.

To create a Rails application, run the rails command and pass in the name of your application. Our application will be called "status".

$ rails status create create app/controllers create app/helpers create app/models create app/views/layouts create config/environments …

A Rails application needs at least two parts: a controller and a view. Our controller will get information about the system, and our view will display it.

You can generate a controller and the corresponding view with the generate script. The following invocation defines a controller and view that implement a single action called index. This will be the main (and only) screen of our application.

$ cd status $ ./script/generate controller status index exists app/controllers/ exists app/helpers/ create app/views/status exists test/functional/ create app/controllers/status_controller.rb create test/functional/status_controller_test.rb create app/helpers/status_helper.rb create app/views/status/index.rhtml

The generated controller is in the Ruby source file app/controllers/status_controller.rb. That file defines a class StatusController that implements the index action as an empty method called index. Fill out the index method so that it exposes the objects you want to use in the view:

class StatusController < ApplicationController def index # This variable won be accessible to the view, since it is local # to this method time = Time.now # These variables will be accessible in the view, since they are # instance variables of the StatusController. @time = time @ps = ps aux end end

The generated view is in app/views/status/index.rhtml. It starts out as a static HTML snippet. Change it to an ERb template that uses the instance variables set in StatusController#index:

Processes running at <%= @time %>

<%= @ps %>

Now our application is complete. To run it, start up the Rails server with the following command:

$ ./script/server => Booting WEBrick… => Rails application started on http://0.0.0.0:3000 => Ctrl-C to shutdown server; call with --help for options …

You can see the application by visiting http://localhost:3000/status/.

Of course, you wouldn expose this application to the outside world because it might give an attacker information about your system.

Discussion

The first thing you should notice about a Rails application is that you do not create separate code files for every URL. Rails uses an architecture in which the controller (a Ruby source file) and a view (an ERb template in an .rhtml file) team up to serve a number of actions. Each action handles some of the URLs on your site.

Consider a URL like http://www.example.com/hello/world. To serve that URL in your Rails application, youd create a hello controller and give it an action called world.

$ ./script/generate controller hello world

Your controller class would have a world method, and your views/hello directory would have a world.rhtml file containing the view.

class HelloController < ApplicationController def world end end

Visiting http://www.example.com/hello/world would invoke the HelloController#world method, interpret the world.rhtml template to obtain some HTML output, and serve that output to the client.

The default action for a controller is index, just as the default page in a directory of a static web server is index.html. So visiting http://www.example.com/hello/ is the same as visiting http://www.example.com/hello/index/.

As mentioned above, a view file is only the main snippet of the final page served by Rails. Its not a full HTML page, and you should never put or tags inside it (see Recipe 15.3). Since a view file is an ERB template, you should also never call puts or print inside a view. ERB was introduced in Recipe 1.3, but its worth exploring here within the context of a Rails application.

To insert the value of a Ruby expression into an ERB template, use the <%= %> directive. Heres a possible world.rhtml view for our hello action:

Several increasingly silly ways of displaying "Hello world!":

<%= "Hello world!" %>

<%= "Hello" + "world!" %>

<%= w = "world" "Hello #{w}!" %>

<%= H + ?e.chr + (l * 2) %><%=(o word!).gsub(d, ld)%>

The last example is excessive, but it proves a point. You shouldn have to put so much Ruby code in your view template (it should probably go into your controller, or youll end up with sloppy PHP-like code), but its possible if you need to do it.

The equals sign in the ERb directive means that the output is to be printed. If you want to execute a command without output, omit the equals sign and use the <% %> directive.

<% hello = "Hello" %> <% world = "world!" %> <%= hello %> <%= world %>

A view and a controller may be based on nothing more than some data obtained from within Ruby code (like the current time and the output of ps aux). But most real-world views and controllers are based on a model: a set of database tables containing data that the view displays and the controller manipulates. This is the famous "Model-View-Controller" architecture, and its by no means unique to Rails.

See Also

Категории