Agile Web Development with Rails

Start from the beginning
                                        

Ican�fthelpit.Ijusthaveto write a Hello,World! programtotrya new system. The equivalentinRails wouldbe an applicationthatsends our cheerygreeting to abrowser.

As we saw in Chapter 2, The Architecture of Rails Applications, on page 22, Rails is a Model-View-Controller framework. Rails accepts incoming requests from a browser, decodes the request to .nd a controller, and calls an action method in that controller. The controller then invokes a particular view to display theresultstotheuser.Thegood newsisthatRailstakescareof most of theinternalplumbingthatlinks allthese actions.To write our simple Hello,

Report erratum

Prepared exclusively for Jordan A. Fowler

HELLO, RAILS!

World! application, we need code for a controller and a view. We don�ft need codefor a model,because we�fre notdealing with anydata.Let�fs start with the controller.

In the same way that we used the rails command to create a new Rails appli-cation, we can also use a generator script to create a new controller for our project.This commandis calledgenerate, anditlivesin the script subdirectory of the demo project we created. So, to create a controller called Say, we make sure we�fre in the demo directory and run the script, passing in the name of the controller we want to create.3

demo> ruby script/generate controller Say

exists app/controllers/

exists app/helpers/

create app/views/say

exists test/functional/

create app/controllers/say_controller.rb

create test/functional/say_controller_test.rb

create app/helpers/say_helper.rb

The script logs the .les and directories it examines, noting when it adds new Ruby scripts or directories to your application. For now, we�fre interested in one of these scripts and(in a minute) the newdirectory.

The source .le we�fll be looking at is the controller. You�fll .nd it in the .le app/controllers/say_controller.rb. Let�fshave alook atit. de.ning classes

Download work/demo1/app/controllers/say_controller.rb page 634 ��.

class SayController < ApplicationController

end

Pretty minimal, eh? SayController is an empty class that inherits from Applica-tionController, so it automatically gets all the default controller behavior. Let�fs spiceit up.We needto add some codetohave our controllerhandletheincom-ingrequest.Whatdoesthis codehavetodo?For now,it�flldo nothing.we sim-ply need an empty action method. So the next question is, what should this method be called? And to answer this question, we need to look at the way Railshandles requests.

Rails and Request URLs

Like any other web application, a Rails application appears to its users to be associated with a URL. When you point your browser at that URL, you are talking to the application code, whichgenerates a response toyou.

3. The concept of the �gname of the controller�h is actually more complex than you might think, and we�fll explain it in detail in Section 14.4, Naming Conventions,onpage 240.For now,let�fsjust assume the controller is called Say.

Report erratum

Prepared exclusively for Jordan A. Fowler

HELLO, RAILS!

http://pragprog.com/say/hello

3. and the action (hello)

Figure4.2:URLsAreMapped toControllers andActions

You've reached the end of published parts.

⏰ Last updated: Mar 22, 2008 ⏰

Add this story to your Library to get notified about new parts!

Agile Web Development with RailsWhere stories live. Discover now