Home arrow Ruby-on-Rails arrow Page 2 - Action Pack and Action View
RUBY-ON-RAILS

Action Pack and Action View


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).

Author Info:
By: Apress Publishing
Rating: 5 stars5 stars5 stars5 stars5 stars / 1
February 16, 2011
TABLE OF CONTENTS:
  1. · Action Pack and Action View
  2. · Embedded Ruby

print this article
SEARCH DEVARTICLES

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.

Helpers

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.

blog comments powered by Disqus
RUBY-ON-RAILS ARTICLES

- Ruby-on-Rails Faces Second Security Flaw in ...
- Ruby 2.0 Prepped for February 2013 Release
- Why LinkedIn Switched from Ruby on Rails
- Adding Style with Action Pack
- Handling HTML in Templates with Action Pack
- Filters, Controllers and Helpers in Action P...
- Action Pack and Controller Filters
- Action Pack Categories and Events
- Logging Out, Events and Templates with Actio...
- Action Pack Sessions and Architecture
- More on Action Pack Partial Templates
- Action Pack Partial Templates
- Displaying Error Messages with the Action Pa...
- Action Pack Request Parameters
- Creating an Action Pack Registration Form

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials