In this second part of a multi-part series on the Action Pack library in Ruby-on-Rails, you'll learn about Action View and the way its templates use Embedded Ruby code (ERb). This article is excerpted from chapter six of the book Beginning Rails: From Novice to Professional, written by Jeffery Allan Hardy, Cloves Carneiro Jr. and Hampton Catlin (Apress; ISBN: 1590596862).
Action Pack and Action View - Embedded Ruby (Page 2 of 2 )
The codes you see mixed into the HTML markup are actually Ruby. Since templates that are capable of dealing only with static HTML wouldnít be very useful, Action View templates have the benefit of being able to use Embedded Ruby (ERb) to programmatically enhance them.
Using ERb, you can embed Ruby into your templates and give them the ability to deal with data from the controller to produce well-formed HTML representations. ERb is included in the Ruby standard library, and Rails makes extensive use of it. You trigger ERb by using embeddings such as<% %>and<%= %>in your template files to evaluate or print Ruby code, respectively. If youíve ever worked with ASP, JSP, or PHP, this style of embedding should be familiar to you.
In the example in the preceding section, the loop is constructed within evaluation embedding tags (<% %>), and the eventís title is printed using output embedding tags (<%= %>). Pay close attention to the subtle difference between the two embedding types: output embedding includes an equal sign; regular embedding does not. When you use output embedding, youíre effectively saying print the results of the Ruby code when itís evaluated. Regular embedding does not print results; it simply evaluates whatever is in between the tags and goes on its merry way. If you mistakenly omitted the equal sign, no errors would be raised, but nothing would be printed either. You would have a set of empty list tags.
The terms of the MVC are fairly strict in the way they advocate the separation of components. Controllers really shouldnít concern themselves with the generation of view code, and views shouldnít concern themselves with anything but the simplest of logic. While itís possible to use ERb to execute arbitrary Ruby code inside a view, and while controllers are certainly capable of generating markup, itís generally considered in violation of the MVC pattern to do so. This is where helpers come in to play.
Action Packís helpers do exactly what their name implies: they help views by providing a convenient location to encapsulate code that would otherwise clutter the view and violate the terms of the MVC. They offer a middle ground between controllers and views and help to keep your application organized and easy to maintain.
If you think about it, ERb tags really arenít the best place for performing complex logic, and templates can quickly become unwieldy when creating markup programmatically. For this reason, Action Pack includes a large suite of built-in helpers for generating all sorts of HTML fragmentsófrom creating forms and formatting dates, to making hyperlinks and image tags. And when the built-in helpers arenít enough, you can write your own. Each controller gets its own helper module thatís mixed in automatically, ready to lend your templates a hand when they need it.
Please check back for the next part of the series.
DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.