Rails and View Helpers

It’s week 4 of my coding bootcamp and we’ve reached the much-hyped and much-anticipated unit of Rails! We’ve spent the last two weeks wading through the nitty-gritty of building of web apps using lower-level Ruby web tools like Rack and Sinatra. It’s important to understand what’s going on behind-the-scenes so that Rails magic doesn’t look like, well, magic. We understand how HTTP works, how to connect and save to a database, the basic mechanics of ActiveRecord, and how to manually build out a Model-View-Controller paradigm. We’ve increased the layers of abstraction until we landed in our Rails happy place. I’m eager to stay here for a little while.

Life before Rails. Source: https://www.cs.cmu.edu/~cangiuli/programming.html

Life before helper methods.

Ruby on Rails is one of the most powerful, dynamic, and effective web application frameworks available. Although I’ve barely waded into its capabilities, I can already see how it makes building web applications much easier. Rails makes assumptions for what you need to start writing your program so you can dive into the best part of programming – building cool stuff.

One of the most helpful things I’ve found about Rails is that it ships with an extensive list of view helpers. Here’s how they work: Web requests are handled by Action Controllers and Action Views. The controller is responsible for communicating with the database and performing CRUD actions. The view compiles the response and renders whatever is sent from the database.

View templates are written with embedded Ruby code and html. So that the view templates remain uncluttered and non-repetitive, Rails has a significant number of helper methods that provide common behavior to forms, dates, and strings. They’re also easy to use as your program grows.

Here are some of the more useful ones I found:


Instead of writing the full messy image, link, and stylesheet html code, this module provides methods for generating the HTML in a minimal manner.

image_url – computes the URL to an image in the app/assets/images directory.


image_tag – returns an HTML image tag (source can be a full path or a file that exists in the app/assets/images directory).


javascript_include_tag – returns an HTML script tag for each of the sources provided. You can pass in the filename (.js extension is optional) or the full path for JavaScript files.


stylesheet_link_tag – returns a stylesheet link tag for the sources specified as arguments. If you don’t specify an extension, .css will be added automatically.


Form Helpers

Who likes creating forms in HTML? NO ONE.

form_tag – when called without arguments, creates a form tag that will POST to the current page.


This would be a generic search form:


check_box – returns a checkbox tag tailored for accessing a specified attribute.


radio_button – returns a radio button tag for accessing a specified attribute.


Other form controls: textareas, password fields, hidden fields, search fields, telephone fields, date fields, time fields, color fields, datetime fields, datetime-local fields, month fields, week fields, URL fields, email fields, number fields and range fields.

Date Helpers

date_select – returns a set of select tags (one for year, month, and day) pre-selected for accessing a specified date-based attribute.


select_date – returns a set of HTML select-tags (one for year, month, and day) pre-selected with the date provided.


Here’s the API documentation with a full list of helpers.


Leave a Reply

Your email address will not be published. Required fields are marked *