Create Prettier Views with Custom Rails Helpers

214 days ago
tags:

While the development community loves to preach separation of logic and markup, so rarely do is this advice followed. Historically though, I don’t think the problem has been due to laziness, smugness, or anything of the sort; rather, many haven’t followed this practice as closely as possible because doing so was hard.

The Rails framework goes a long way towards eliminating this coder’s faux pas, not only by way of its MVC implementation, but also by offering numerous features which facilitate the removal of localized logic from view templates. One of the best examples of such a feature are Rails helpers, which are essentially a bunch of utilities that make it easier and cleaner to embed logic within a template by way of what you could essentially think of as macro calls.

Rails comes bundled with quite a few helpers capable of automating tasks such as pagination, creating form fields, and embedding assets such as JavaScript and CSS files. You can view a list of links to helpers found throughout the documentation here.

Yet occasionally you’ll be looking for a helper that isn’t bundled with the documentation, one capable of abstracting a task you’ll be using repeatedly in a view. I recently ran into this problem while working on a new Rails-driven website that serves as a directory for automobile dealers. Each dealer’s detailed view contains a list of key characteristics such as whether they’re open on Sunday, whether they’ll allow you to test drive a car, and whether they’re salesmen are particularly annoying. But rather than provide a simple yes/no list, I wanted to use green and red stoplights as visual indicators (get it, cars, stop lights??) for each.

While you could embed three if statement in the view to check the object attribute, a ternary statement would at least look a bit cleaner:


<b>Open Sunday</b>: 
<%= @dealer.seating ? 
   "<img src='/images/green.png' />" : 
   "<img src='/images/red.png' />" %><br />
<b>Test Drive</b>: 
   <%= @dealer.testdrive ? 
      "<img src='/images/green.png' />" : 
      "<img src='/images/red.png' />" %><br />
<b>Annoying</b>: 
      <%= @dealer.annoying ? 
      "<img src='/images/green.png' />" : 
      "<img src='/images/red.png' />" %><br />

However, such code is still unpleasant to find in a view, not to mention difficult to maintain. The solution? A custom helper!

To solve this problem, in the dealer controller’s corresponding helper file (dealer_helper.rb) I created the following method:


def dealer_option_tag(asset)
    asset ? image_tag("green.png") : image_tag("red.png")
end

Notice I took advantage of another helper named image_tag, which will produce the same img tag as found in the previous ternary statement, but in a much cleaner fashion in respect to the code.


<b>Open Sunday</b>: 
<%= dealer_option_tag(@dealer.delivery) %><br />
<b>Test Drive</b>: 
<%= dealer_option_tag(@dealer.seating) %><br />
<b>Annoying</b>: 
<%= dealer_option_tag(@dealer.annoying) %><br />

As you can see, not only is this approach much easier to maintain, but also a more natural bridge for a designer!

Jason



Manage Your MySQL Database with Rails Migrations

235 days ago
tags: ,

The first article of my new Developer.com series covering Rails’ powerful database features introduces you to Migrations, a great tool allowing developers to manage database schema changes in much the same way they manage code.

Read the article



Ruby on Rails -vs- Django

241 days ago
tags: ,

These guys kill me, but this episode in particular is truly a riot:

Watch the video



Pro Active Record Published

242 days ago
tags:

I’ve very pleased to announce that the latest title in our growing Ruby and Rails series has published: Pro Active Record: Databases with Ruby and Rails.

In Pro Active Record, authors Kevin Marshall, Chad Pytel, and Jon Yurek walk you through every step from the basics of getting and installing the Active Record library to working with legacy schema to using features specific to each of today’s most popular database engines, including Oracle, MS SQL, MySQL, and more!

You’ll come to a deep understanding of Active Record that will enable you to truly exploit all that Ruby, Rails, and your chosen database platform have to offer.

Jason



Five Questions for Andre Lewis

439 days ago

I had the pleasure of chatting with Andre Lewis, lead author of “Beginning Google Maps Applications with Rails and Ajax”. We talked about his fascinating array of cool Rails-driven online services and projects he’s spearheading, the new book, and tips for budding Rails developers.


Click here to read more


Ruby In Steel - Ruby/Rails IDE for VS 2005

442 days ago
tags: ,

Huw Collingbourne wrote this afternoon to let us know about Ruby In Steel, a Ruby/Rails IDE for Visual Studio 2005. There’s a free and commercial version, with both offering quite an impressive array of features.

Learn more about Sapphire In Steel



"How has Rails made me a better programmer" Blogging Winner Announced

457 days ago
tags: ,

I’m very pleased to announce we’ve selected a winner for the “How has Rails made me a better programmer” blogging contest, which Apress ran in conjunction On Ruby weblog, authored by the indefatigable Pat Eyler. Apress author Jarkko Laine also headed up the judging.

Congratulations to Christian Neukirchen for essay, linked here.

You can learn more about the contest outcome at the On Ruby website.

Jason



Beginning Rails E-Commerce

557 days ago
tags:

I’m pleased to announce that Beginning Ruby on Rails E-Commerce is now available (Apress, ISBN: 1590597362).

Written by the highly-talented Rails gurus Christian Hellsten and Jarkko Laine, the book explains everything you need to know about building a Web 2.0-style online shop using Rails. It’s practical and hands-on throughout, rather than theoretical, and Christian and Jarkko have done a superb job. Put simply, it’s a great book.

It’s available from Amazon as well as all good bookstores. You can also read a sample chapter, and view the table of contents, at the book’s page at the Apress website.

Over at the On Ruby blog, Pat Eyler has interviewed Jarkko, who explains a little about himself and his goals when writing the book.



David Heinemeier Hansson: 34th Person Who Matters

641 days ago
tags:

Top of the list of happenings I somehow missed in June, but still worth mentioning now, is the fact that the creator of Ruby on Rails, David Heinemeier Hansson, made it to no 34 in the Business 2.0 list of 50 People Who Matter Now. Rather entertainingly he’s one ahead of Tim O’Reilly, but still 34 places behind myself (and you, and your family, and everybody you know, because the number one person is “You: The consumer as creator”. Cute.)

That’s not bad for somebody who was, until just a few years ago, an unknown software developer. But even the fact that David is included in the list shows just how much Rails and “Web 2.0” technologies matter right now. People outside of computing are taking notice.

To quote from the article:

[Rails] has made it dramatically faster and cheaper to build dynamic websites—a transformation that’s done much to help make today’s crop of Web 2.0 startups so successful [...] The future of software looks a lot less cumbersome, and for that we have Hansson to thank.


An interview with Peter Cooper, author of Beginning Ruby

655 days ago

Peter Cooper is a 24 year-old developer and trainer who’s at the forefront of the British Ruby and Ruby on Rails scene. He’s also in the middle of writing Beginning Ruby for Apress, our foundation Ruby title, which will form the cornerstone of Apress’ Ruby and Rails line that will hit the market later this year.

I caught up with him today for a chat.


Click here to read more


The spirit of Rails

691 days ago
tags: , ,

Part of my job at Apress is to commission books on open source web development. Without any doubt the hottest topic right now is Ruby on Rails. I’m working with a number of authors on book titles and they’re all remarkably similar in that they’re passionate and forthright individuals. In fact, my experience has shown that there’s definitely a “Rails mindset” that’s quite different from anything that’s gone before. Rails is about far more than making websites.

In his blog posting on notrocketsurgery.com, Ryan Norbauer critically responds to a speech by Dave Thomas at RailsConf, but in doing so neatly outlines the Rails philosophy. Quote:

What has been most surprising for me at RailsConf has been the extent to which so many people just don’t get what makes Rails Rails. The Rails framework is not just a new toy; it’s a radical new philosophy of web development—and of programming in general.

Read the blog posting here.