Aswiththe .rst edition,theRailscoreteamwasincrediblyhelpful,answering questions,checkingout codefragments,and .xingbugs.Abig thankyou to
ScottBarron(htonl),JamisBuck(minam),ThomasFuchs(madrobby),
JeremyKemper(bitsweat),MichaelKoziarski(nzkoz),
Report erratum
Prepared exclusively for Jordan A. Fowler
ACKNOWLEDGMENTS
MarcelMolinaJr,(noradio),RickOlson(technoweenie),
NicholasSeckar(Ulysses),SamStephenson(sam),TobiasLutke(xal),
andFlorianWeber(csshsh).
I�fd like to thank the folks who contributed the specialized chapters to the book: Leon Breedt, Mike Clark, James Duncan Davidson, Justin Gehtland, andAndreasSchwarz.
Ikeeppromising myself that eachbook willbethelast,iffor no other reason than each takes me away from my family for months at a time. Once again: Juliet,Zachary, andHenry.thankyoufor everything.
DaveThomas November 2006
dave@pragprog.com
�gAgile WebDevelopment with Rails...Ifoundit in ourlocalbookstoreandit seemedgreat!�h .Dave�fsMum
Report erratum
Prepared exclusively for Jordan A. Fowler
Part I
Getting Started
Prepared exclusively for Jordan A. Fowler
Chapter2
TheArchitectureofRailsApplications
One of the interesting features of Rails is that it imposes some fairly serious constraints on how you structure your web applications. Surprisingly, these constraints makeit easier to create applications.alot easier.Let�fs see why.
2.1 Models, Views, and Controllers
Backin1979,TrygveReenskaugcame up with a new architecturefordevelop-inginteractive applications.Inhisdesign, applications werebrokeninto three types of components: models, views, and controllers.
The model is responsible for maintaining the state of the application. Some-timesthis stateistransient,lastingforjust a couple ofinteractions with the user. Sometimes the state is permanent and will be stored outside the appli-cation, oftenin adatabase.
A modelis morethanjustdata; it enforces all thebusiness rulesthat apply to that data. For example, if a discount shouldn�ft be applied to orders of less than$20, the model will enforce the constraint.This makes sense;byputting the implementation of these business rules in the model, we make sure that nothing else in the application can make our data invalid. The model acts as both agatekeeper and adata store.
The view is responsible for generating a user interface, normally based on data in the model. For example, an online store will have a list of products tobedisplayed on a catalog screen.Thislist willbe accessible viathe model, but it will be a view that accesses the list from the model and formats it for the end user. Although the view may present the user with various ways of inputting data, the view itself never handles incoming data. The view�fs work isdoneoncethedataisdisplayed.Theremay wellbe many viewsthat access the same modeldata, oftenfordifferentpurposes.Inthe online store,there�fll
Prepared exclusively for Jordan A. Fowler
MODELS, VIEWS, AND CONTROLLERS
..Browser.sends.request
Agile Web Development with Rails
Start from the beginning
