However, the real situation is somewhat more complicated than that. Let�fs imagine that your application is available at the URL http://pragprog.com/. The web server that is hosting your application is fairly smart about paths. It knows that incoming requests to this URL must be talking to the applica-tion. Anything past this in the incoming URL will not change that.the same application will still be invoked. Any additional path information is passed to the application, which can useitforits owninternalpurposes.
Rails uses the path to determine the name of the controller to use and the name of the action to invoke on that controller.4 This is illustrated in Fig-ure 4.2.The .rstpart of thepathisthenameof thecontroller,and thesecond part is the name of the action. This is shown in Figure 4.3, on the following page.
Our First Action
Let�fs addan action called hello to our say controller.Fromthediscussioninthe previous section, weknow thatadding a hello action means creating a method called hello in the classSayController.But what shoulditdo?For now,itdoesn�ft havetodo anything.Rememberthat a controller�fsjobisto set upthings so that the view knows what to display. In our .rst application, there�fs nothing toset up,soanempty actionwill work.ne.Useyourfavoriteeditortochange methods
page 632 ��.
the .le say_controller.rb in the app/controllers directory, adding the hello method as shown.
Download work/demo1/app/controllers/say_controller.rb class SayController < ApplicationController
def hello
end
end
4. Railsisfairly.exiblewhenit comestoparsingincomingURLs.Inthischapter,wedescribethe defaultmechanism.We�fll showhowto override thisinSection 20.2, RoutingRequests, onpage 393.
Report erratum
Prepared exclusively for Jordan A. Fowler
HELLO, RAILS!
http://pragprog.com/say/hello
Figure4.3:RailsRoutes toControllers andActions
Now let�fs try calling it. Navigate to the URL http://localhost:3000/say/hello in a browser window.(Notethatinthedevelopmentenvironment wedon�fthave any application string at thefront of thepath.we routedirectly to the controller.) You�fll see something thatlookslike thefollowing.
It might be annoying, but the error is perfectly reasonable (apart from the weird path). We created the controller class and the action method, but we haven�fttoldRails whattodisplay.Andthat�fs wherethe views comein.Remem-ber when we ran the script to create the new controller?The command added three .les and anewdirectory to ourapplication.Thatdirectory containsthe template .les for the controller�fs views. In our case, we created a controller named say, so the views willbein thedirectory app/views/say.
To complete our Hello, World! application, let�fs create a template. By default, Rails looks for templates in a .le with the same name as the action it�fs han-dling. In our case, that means we need to create a .le called hello.rhtml in the directory app/views/say.(Why .rhtml? We�fll explain in a minute.) For now, let�fs justputsomebasicHTMLin there.
Report erratum
Prepared exclusively for Jordan A. Fowler
HELLO, RAILS!
Figure4.4:StandardLocationsforControllers andViews
Download work/demo1/app/views/say/hello.rhtml <html>
<head>
<title>Hello, Rails!</title>
</head>
Agile Web Development with Rails
Start from the beginning
