Agile Web Development with Rails

Start from the beginning
                                        

Active Record is the solid model foundation of the Rails MVC architecture. That�fs why wedevote three chapters toit, starting onpage 283.

2.3 Action Pack: The View and Controller

When you think about it, the view and controller parts of MVC are pretty intimate. The controller supplies data to the view, and the controller receives events from the pages generated by the views. Because of these interactions, supportfor views and controllersinRailsisbundledinto a single component, ActionPack.

Don�ft be fooled into thinking that your application�fs view code and controller code willbejumbled upjustbecauseActionPackis a single component.Quite the contrary;Railsgivesyouthe separationyou needto write web applications with clearlydemarcated codefor control andpresentationlogic.

View Support

In Rails, the view is responsible for creating either all or part of a page to be displayedin abrowser.3Atits simplest, a viewis a chunk ofHTML codethat displayssome .xedtext.Moretypicallyyou�fllwanttoincludedynamiccontent createdby the action methodin the controller.

InRails,dynamiccontentisgeneratedby templates,which comeinthree .a-vors. The most common templating scheme, called rhtml, embeds snippets of Rubycode withinthe view�fsHTML using aRubytool calledERb(orEmbedded Ruby).4 This approach is very .exible, but purists sometimes complain that it violates the spirit of MVC. By embedding code in the view we risk adding logic that should be in the model or the controller. This complaint is largely

3.

OranXML response,orane-mail,or....Thekeypointisthat viewsgeneratetheresponseback to the user.

4.

Thisapproach mightbefamiliartowebdevelopersworking withPHP orJava�fsJSP technology.

Report erratum

Prepared exclusively for Jordan A. Fowler

groundless: views contained active code even in the original MVC architec-tures. Maintaining a clean separation of concerns is part of the job of the developer.(Welook atHTML templatesinSection 22.1, RHTML Templates, on page468.)

The secondtemplatingscheme, calledrxml,letsyou constructXMLdocuments usingRuby code.the structure ofthegeneratedXML will automaticallyfollow the structure of the code.Wediscuss rxml templates starting onpage 467.

Rails also provides rjs views. These allow you to create JavaScript fragments on the server that are then executed on thebrowser.Thisisgreatfor creating dynamicAjaxinterfaces.We talk about these starting onpage 558.

And the Controller!

TheRails controlleris thelogicalcenter ofyour application.It coordinates the interactionbetweenthe user,the views, andthe model.However,Railshandles most of thisinteractionbehind the scenes; the codeyou write concentrates on application-level functionality. This makes Rails controller code remarkably easy todevelop and maintain.

The controlleris alsohome to a number ofimportant ancillary services.

.

Itisresponsibleforroutingexternal requeststointernal actions.Ithan-dlespeople-friendlyURLs extremely well.

.

It manages caching, which can give applications orders-of-magnitude performanceboosts.

.

It manages helper modules, which extend the capabilities of the view templates withoutbulking up their code.

.

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