Home arrow Ruby-on-Rails arrow Page 2 - Filters, Controllers and Helpers in Action Pack

Filters, Controllers and Helpers in Action Pack

In this sixteenth part of an eighteen-part series on the Action Pack library for Rails, you'll learn how to apply filters to controllers and how to use Action View helpers. 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 / 5
July 28, 2011
  1. · Filters, Controllers and Helpers in Action Pack
  2. · Using Action View Helpers

print this article

Filters, Controllers and Helpers in Action Pack - Using Action View Helpers
(Page 2 of 2 )

One of the ways we can clean up our templates is with helpers. Rails ships with a bevy of formatting helpers to assist in displaying numbers, dates, tags, and text in your tem plates. Here’s a quick summary:

Number helpers: The NumberHelper module provides methods for converting

numbers into formatted strings. Methods are provided for phone numbers,

currency, percentage, precision, positional notation, and file size. See http://

api.rubyonrails.org/classes/ActionView/Helpers/NumberHelper.html for more


Text helpers: The TextHelper module provides a set of methods for filtering, formatting, and transforming strings that can reduce the amount of in-line Ruby code in your views. See http://api.rubyonrails.org/classes/ActionView/Helpers/ TextHelper.html for more information.

URL helpers: Rails provides a set of URL helpers that make constructing links that depend on the controller and action (or other parameters) ridiculously easy. For more information, see http://api.rubyonrails.org/classes/ActionView/Helpers/ UrlHelper.html and http://api.rubyonrails.org/classes/ActionController/Base.html .

Let’s take a closer look at two very handy URL helpers that you’re likely to use often: url_for and link_to .

The url_for method returns a URL that has been rewritten according to the given options hash and the defined routes. It has the following format:


You can provide the following options:

• :anchor : Specifies the anchor name to be appended to the path. For example, url_for :controller => 'posts', :action => 'show', :id => 10, :anchor => 'comments' will produce "/posts/show/10#comments" .

• :only_path : If true , returns the relative URL (omitting the protocol, hostname, and port). It’s false by default.

• :trailing_slash : If true , adds a trailing slash, as in /pub/archive/2007/ .

  1. :host : Overrides the default (current) host if provided.
  2. :protocol : Overrides the default (current) protocol if provided.

The URL will be generated from the remaining keys in the hash, and Routes will compose a query string for any key/value pairs not included in the route definition. For example, if you had a route defined as /events/show/:id and passed in additional parameters to url_for , such as print => true , the extra parameters would be rendered as http://example.com/events/show/1/?print=true .

The default routes setup supports a typical Rails path of controller/action/id , where action and id are optional, with action defaulting to index when not given. Here are some typical url_for statements and their corresponding URLs:

url_for :controller => 'posts', :action => 'recent'

# => 'http://example.com/posts/recent' url_for :controller => 'posts', :action => 'index'

# => 'http://example.com/posts' url_for :controller => 'posts', :action => 'show', :id => 10

# => 'http://example.com/posts/show/10' url_for :controller => 'posts', :action => 'show', :id => 10,

:print => true, :return => false

# => 'http://example.com/posts/show/10/?print=true&return=false'

When generating a new URL, missing values may be filled in from the current request’s parameters. For example, url_for :action => 'some_action' will retain the current controller, as expected. This behavior extends to other parameters, including :controller , :id , and any other parameters that are placed into a route’s path.

Another handy URL helper is link_to , which creates a hyperlink tag of the given name using a URL constructed according to the options hash given. It acts exactly like url_for , except that it produces an HTML hyperlink. It’s possible to pass a string instead of an options hash to get a link tag that points to any URL. Additionally, if nil is passed as a name, the link itself will become the name. Here’s the fine print:

link_to(name, link={}, options={})

This generates an HTML anchor tag using the following parameters:

  1. The first argument is the link’s name.
  2. The second argument is the URL to link to, given either as a string or a hash of options used to generate the URL.
  3. The third argument is a hash of options for the resulting tag.

In Ruby, if the last argument to a method is a hash, the curly braces are optional. Most link_to helpers will therefore look like this:

link_to 'New', :controller => 'events', :action => 'new'

or, if using a named route, like this:

link_to 'New', new_event_url

If you’re using all three arguments and are passing in options for HTML (like a class or id attribute), you need to disambiguate them. Consider the following example where we’re using two hashes: one for the URL generation and another for the HTML options:

link_to 'New', {:controller => 'articles', :action => 'new'}, :class => 'large'

Notice that we need to use the curly braces for at least the first hash to inform Ruby that there are three arguments. Using braces on the last hash of options is still optional, and we could just have easily included them:

link_to 'New', {:controller => 'articles', :action => 'new'}, {:class => 'large'}

Please check back regularly for the continuation of this 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 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 

Developer Shed Affiliates


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