Archive Page 3

20
Dec

Nested URL Parameters

One of the great things about Rails is the ability to wire together form logic with extreme ease through Rails’ support of essentially representing hashes through the “object[name]” syntax of URL parameters. Arrays are also supported in a similar manner, making things like many-to-many relationship management cake.

In Rails 1.2 one issue I ran into is that this hash-based access logic cannot be nested when using helpers such as url_for (which is in turned used by helpers such as link_to, etc.) This type of functionality is rather useful when you are trying to encapsulate a set of name-value pairs within one “thing”, such as a “search” which is many criteria => value pairs. For a particularly project of mine I established a pretty nice design pattern for working with searches and results in a very abstract manner, an area of Rails that I generally find underdeveloped with no standard practice.

I was lucky to stumble on a Rails plugin that monkey-patches Rails to support this: nested_params_patch. Information about this plugin is quite sparse but this article does a good job of better-explaining what the plugin does. Essentially it allows you to do things like this:

person_url(:name => {0 => 'Ryan', 1 => 'Kinderman})

Pretty cool stuff! I am always amazed and the kind of plugins and extensions that are possible, all resulting from Ruby’s dynamicism and Rails’ heavily extensible architecture.

11
Dec

User Alert Management with flash and ActiveRecord::Errors

A very common paradigm in web application development is presenting the user with some sort of alert or flash message at the top of a page. These alerts are often styled with a background and some sort of icon to the left to indicate what type of error it is. Typically there are a few different types of messages such as “error”, “warning”, “confirmation”, etc., perhaps each type styled differently. Furthermore, such a block typically should support showing a set of messages within one type; not just one string. In Rails the two sources of these messages are usually either the flash object (such as flash[:error]) or ActiveRecord validations errors.

Here is an example of what I mean:

I’ve created a helper method that makes working with these types of messages a breeze.

Essentially it allows you to use the flash object with any of the following keys to specify messages: error, confirm, back, info, and warn (though you can modify it to suit any group of message types). You can either assign a string directly to this which is common, or create it as an array with multiple string messages. The helper will then read these flash objects and display it as a message block that can be styled with CSS, supporting multiple messages per type, separated by type, as well as dynamically grabbing ActiveRecord validation errors off of model object(s) of your choosing and showing those also. The usage of the helper method in your views is as follows

# In Controller:
flash[:confirm] = "Thank you for your input."
flash[:error] = []
flash[:error] << "Wrong Address"
flash[:error] << "Wrong Name"

# In View:
<%= message_block %>

# Or if you want to display errors on @customer
# as well as the flash messages,
<%= message_block :o n => :customer %>

# You can even have it watch multiple model
# objects for errors:
<%= message_block :o n => [:customer, :o rder] %>
Here is the helper method that makes this happen:

# Outputs the error messages block.
# The first argument specifies a hash options:
# * :o n => :products   Also includes AR validation errors for @products
# * :clear => true     Clears messages after displaying
# * :keep => true      Keeps around messages for next response cycle
#
def message_block(options = {})
  out = ""

  [:back, :confirm, :error, :info, :warn].each do |type|
    next if flash[type].nil? or flash[type].empty?
    flash[type] = [flash[type]] unless flash[type].is_a?(Array)

    out << "<div class=\"container #{type}\"><ul>\n"
    flash[type].each {|msg| out << "<li>#{h(msg.to_s)}</li>\n"}
    out << "</ul></div>\n"

    flash[type] = nil if options[:clear]
    flash.keep[type] if options[:keep]
  end

  if options[:on]
    options[:on] = [options[:on]] unless options[:on].kind_of?(Array)
    models = options[:on].map {|m| instance_variable_get("@" + m.to_s)}.select {|m| !m.nil?}
    errors = models.inject([]) {|b,m| b += m.errors.full_messages}

    if errors.size > 0
      out << "<div class=\"container error\"><ul>\n"
      errors.each {|msg| out << "<li>#{h(msg.to_s)}</li>\n"}
      out << "</ul></div>\n"
    end
  end

  content_tag(:div, out, :id => 'message_block', :class => 'flash')
end

And the CSS code that works with it:

/*** Flash Messages ***/
.flash {

}

.flash ul {
    padding-left: 0pt;
    margin-bottom: 0pt;
    list-style-type: none;
    margin-left: 0pt;
}

.flash ul li {
    background: transparent url(../images/icons/bullets/gt.gif) no-repeat scroll left center;
    margin-bottom: 0.6em;
    padding-left: 1em;
    vertical-align: top;
}

.flash .container {
    padding: 1em;
    padding-left: 5em;
    margin-bottom: 1.5em;
}

.flash .error {
    background: #fcf6d0 url(../images/icons/flashes/error.gif) 1.5em 1em no-repeat;
    border-top: 1px solid #ecd757;
    border-bottom: 1px solid #ecd757;
}
.flash .back {
    background: #e9f3dc url(../images/icons/flashes/back.gif) 1.5em 1em no-repeat;
    border-top: 1px solid #bfcbb0;
    border-bottom: 1px solid #bfcbb0;
}

.flash .confirm {
    background: #e9f3dc url(../images/icons/flashes/confirm.gif) 1.5em 1em no-repeat;
    border-top: 1px solid #bfcbb0;
    border-bottom: 1px solid #bfcbb0;
}

.flash .info {
    background: #dee9f4 url(../images/icons/flashes/info.gif) 1.5em 1em no-repeat;
    border-top: 1px solid #b4c5d5;
    border-bottom: 1px solid #b4c5d5;
}

.flash .warn {
    background: #fcf6d0 url(../images/icons/flashes/warn.gif) 1.5em 1em no-repeat;
    border-top: 1px solid #ecd757;
    border-bottom: 1px solid #ecd757;
}

You may even want to consider writing a before_filter in your application controller that sets the flash types to arrays so you can just add messages to them with the << operator:

class ApplicationController < ActionController::Base
  before_filter :initialize_flash_types

  def initialize_flash_types
    [:back, :confirm, :error, :info, :warn].each {|type| flash[type] = []}
  end
end
10
Dec

Don’t use symbols for route parameters!

I’ve ran into this issue more than once: Be sure that you are using strings and not symbols when specifying parameters for routes. Particularly, if you specify a symbol for :action, Rails will fail to see your controller action yet still show your Rails view! This can sometimes be very difficult to diagnose. So, don’t do this:

map.connect '/pages/:slug',
:controller => 'pages',
:action => :show

Do this:

map.connect '/pages/:slug',
:controller => 'pages',
:action => 'show'
09
Dec

HTML-Aware Truncate Text

When building a large custom PHP CMS system for DigitalPeach, I ran into a very difficult issue: truncating text but maintaining HTML nested tags correctly. Specifically, we were looking to breaking up large articles composed using FCKEditor into separate pages after a certain character threshold. Once can easily see the problem:

<p>This is a test <strong>with some bold in here</strong>.</p>

Now imaging having to truncate this text to 30 characters, and you end up with this:

This is a test <strong>with

While this example isn’t quite so severe and at worst would only make the rest of the text within the block-level element bold, clearly if we do a truncation that is blind to HTML some serious problems can arise. Furthermore, even if it doesn’t have much practical significance, you are breaking XHTML.

I was lucky to stumble across an excellent article by Mike Burns who describes a Ruby method using REXML’s pull parser that can accomplish this. His example extended the String class, so I modified it to work as a Rails helper all in one method:

def truncate_html(input, len = 30, extension = "...")
  def attrs_to_s(attrs)
    return '' if attrs.empty?
    attrs.to_a.map { |attr| %{#{attr[0]}="#{attr[1]}"} }.join(' ')
  end

  p = REXML::Parsers::PullParser.new(input)
    tags = []
    new_len = len
    results = ''
    while p.has_next? && new_len > 0
      p_e = p.pull
      case p_e.event_type
    when :start_element
      tags.push p_e[0]
      results << "<#{tags.last} #{attrs_to_s(p_e[1])}>"
    when :end_element
      results << "</#{tags.pop}>"
    when :text
      results << p_e[0].first(new_len)
      new_len -= p_e[0].length
    else
      results << "<!-- #{p_e.inspect} -->"
    end
  end

  tags.reverse.each do |tag|
    results << "</#{tag}>"
  end

  results.to_s + (input.length > len ? extension : '')
end

Note that the nested method above is a completely valid use of Ruby, though not widely used.

And now look at what it can do:

truncate_html("<p>Test <strong>bold</strong> done.</p>", 30)
# => "<p>Test <strong>bold</strong></p>..."
08
Dec

Book Review: The Rails Way

Whenever I go to a bookstore I am sure to check out the Rails/Ruby section (constantly increasing in size!) for any new titles I may not be aware of. I was pleasantly surprised to find a new large Rails book that almost went unnoticed: The Rails Way by Obie Fernandez. I say almost went unnoticed because the book cover looks almost identical to The Ruby Way, so it’s easy to gloss over if you don’t look carefully.

It is easy to see by the sheer size that this book, weighing in at 850 pages, that it is very in-depth. The author explicitly states in the introduction that this is not a book for Ruby on Rails beginners. It was intended on being primarily an information-packed reference book for professional developers working with Ruby on Rails. The most appealing characteristic of the book is that it covers Rails 2.0, the first book to my knowledge that does.

After reading the book (it’s a long read!) I can definately say that this is a very valuable book for Rails developers. Typically after reading the Agile book, Rails developers end up craving knowledge of more advanced Rails techniques and plugins, and this book certainly delivers in this respect. Sprinkled throughout the detailed description of the Rails API are useful plugins, tips, and techniques that the author has come across in his development experience. Also included are many snippets of code from the Rails source code that help explain how key things work in Rails.

A full chapter outline is available online so I won’t bother duplicating that work, but let me point out a few areas of Rails that this book covers:

  • Rails Environment and Configuration – explains the Rails boot process and the automatic class loader, as well as other things behind the scenes that make Rails tick – very interesting.
  • New Rails 2.0 RESTful Routes – Covers some changes and enhancements to the RESTful routing features of Rails 2.0.
  • Advanced ActiveRecord – Really gives detailed information about ActiveRecord, even briefly explaining the AssociationProxy class that masquerades as an Array for those has_many associations.
  • Prototype Reference
  • Selenium Testing
  • RSpec
  • Using subversion with Rails and piston
  • Rails deployment with Capistrano
  • Common production configuration setups
  • Background processing with BackgrounDRb
  • ActiveSupport Reference

The last few topics in particularly were mostly until now the realm of random blog posts online with no solid resource in print.

In short, The Rails Way is a must-have for any developer working professionally with Ruby on Rails. Far from being a programming resource only, it helps you become an all-around better developer by arming you with techniques, new plugins, and tools to improve your productivity. Targeted towards the advanced user, this book is packed full of information that will greatly improve your facility with Rails. Buy it today!

07
Dec

Rails 2.0 Released!

So today Rails 2.0 was finally released; it seems as though the Rails core team has been polishing this up for ages. I don’t think there’s a whole lot to get excited about. In short, Rails 2.0 gives you better opportunity to make use of best practices, such as using the will_paginate plugin instead of build-in pagination (Rails 2.0 drops pagination support from core). Support for RESTful development is much improved, allowing more flexible route declarations and finally dropping the annoying semicolon separator for the edit action: projects/1;edit.

One very cool feature that should better allow developers to present meaningful error messages to users upon exceptions is the new rescue_from class method in ActionController::Base, which allows you to specify an exception class to rescue and an action to call to handle the error.

A few of my dissapointments of Rails 2.0 is that my two most pressing present concerns with Rails were not addressed:

A cohesive way of managing “fixture-like” data in an application. While simple Rails applications can rely on fixtures just fine, real production applications are not quite this simple and may rely on different sets of data that distinguish between semi-permanent “application” data and true test data. Multiple this by the dimension of multiple deployment environments (staging, production) which each may require different sets of data. I typically end up writing my own rake tasks and duplicate the fixtures data for specialized uses. It would be nice to see Rails provide an effective and standardized mechanism for being more flexible with “fixture-like” data rather than forcing us to use one inflexible “fixtures” directory.
Using the :include class in ActiveRecord finders creates severe limitations on the type of data you can get back. There have been numerous situations where I’ve wanted to use :include to get an object tree populated from one query yet still return an aggregation result in the select clause. A perfect example is listing forum threads, showing the number of comments but also including the user data who posted the thread. This is currently impossible with ActiveRecord because the select clause cannot be used with the include clause.
I plan on talking more about these two main issues for me in future posts, hopefully soliciting some responses. I haven’t found anything that adequately addresses my concerns.

Ultimately Rails 2.0 is sprinkled with various small changes rather than a fundamental architectural shift. As a result, it should be fairly easy to upgrade existing applications to Rails 2.0 and would probably be a good idea to do so, especially to force yourself to abandon bad practices that Rails 2.0 is less tolerant of.

One piece of reading that I would highly recommend if you already are familiar with Rails 1.2 and want to get up to speed on Rails 2.0 is the Peepcode PDF available here. I’ve found some of the Peepcode screencasts to be annoying slow an drawn-out, but this PDF is a must-have, packed full with the information you want to know, and quick to read.

02
Dec

Welcome!

I’ve been wanting to start a Ruby on Rails blog for a while to document my experiences using the framework and engage with the community, but always felt I wouldn’t have enough time to maintain it. Today I decided to give in and begin RailsGarden.com.

So just about every developer working with Rails professionally has a life story culminating in total infatuation with the Ruby language and Rails framework; here’s mine:

Having starting programming at an early age with a continual and early interest in computers in general, I quickly latched onto web programming because of the relative ease and the potential to create something decent without a whole lot of work. I first dabbled in Perl and at one point developed a small data-driven e-commerce application that powered a very early version of www.rarenewspapers.com. Looking forward to the newer web technologies of the time, I began learning PHP and MySQL. Relational databases and techniques to structure and store data well has always intrigued me. To this day, modeling databases and advanced database structures are my specialty. I began doing some basic projects for a few clients online while still in high school. The projects I took on increased in complexity as I gained better design patterns and my own personal coding stuff. Most of these projects were for a company I am still deeply involved with, Digital Peach Interactive, based out of Georgia. I also dabbled a bit in Adobe ColdFusion and ASP.NET with SQL Server

My use of PHP to architect web applications culminated in the design and implementation of a large internal content management system used at Digital Peach, implementing advanced features such as a very sophisticated hierarchical category system for permissions and a workflow engine. Building this application with my boss was quite an experience and the data model ended up consisting of over 130 tables, including tables for some of our main modules.

Having done quite a bit with PHP and become pretty proficient in using it and MySQL to build data-driven applications, I wasn’t particularly “happy” with what I did and didn’t derive a lot of intellectual satisfaction out of building software in PHP. This all changed when I began to look at Ruby on Rails, a technology I very ignorantly ignored for so long. My first experience with RoR was viewing the standard make a blog screen cast and a few of the online slide shows. Though initially skeptical of the framework due to scalability and extensibility concerns (due to my superficial understanding), I decided to purchase the standard Agile Development with Ruby on Rails book. Reading this book was like a constant barrage of “aha!” moments, page after page. It would be difficult to find another tech book that had such a high ratio of information to pages; every page is packed full of useful information, ready for immediate application.

Already very excited about the prospects of using Rails as a framework, I still felt inadequate in my understanding of the framework and how it worked under the hood. Everything in Rails to me seemed like magic and it was insufficient for me to know that “has_many” established as one-to-many relationship with another model – I had to know why this was possible. This lead me to pick up the Programming Ruby pickaxe book which is an essential resource for really understanding the Ruby language.

I still am a strong believer in the Ruby language itself. The Rails framework is excellent, but ultimately what allows Rails to be so well-constructed is the extremely dynamic nature of the Ruby language. Engineering software components in Ruby is extremely enjoyable. Sometimes I think that Rails developers put too much emphasis on the Rails side and not enough on the Ruby side. I find that such emphasis also leads a lot of newcomers to Rails with an insufficient understanding of the Ruby language to dismiss the framework for this or that reason. Ultimately a complete and thorough understanding of the Ruby language is essential for appreciating many capabilities of using Rails as a framework for sufficiently large applications.

After these two books which have changed my life, I began reading other Rails/Ruby books to acquire more knowledge and gain better insight into the techniques used my professional Rails developers. Rails blogs were also an essential resource.

The rest, they say, is history! I’ve been using Ruby on Rails ever since, from small to large projects (such as Timothy Hughes Rare & Early Newspapers, which I’ll discuss in the future with this blog), and i’m learning something new every day. Working on the larger projects has really pushed me into new directions with Rails and helped me to establish a much better picture of the tools, plugins, and resources available to me when building software.

And so it is my goal with Rails Garden to document some of the tools and techniques I use in my professional use of Ruby on Rails. Enjoy!





  • Ben Hughes

    I'm a freelance developer working with Ruby and other modern tools to build web applications, based currently out of Rochester, NY. I love to learn about new technologies and am always trying to achieve elegance and beauty through code.

    When I'm not writing software, I like to play tennis, dabble in jazz piano, and ponder economics. I'm a big fan of: world travel and cultures, jazz music, Korean food, coffee, and having interesting conversations.

  • Recommend Me
July 2010
M T W T F S S
« Jan    
 1234
567891011
12131415161718
19202122232425
262728293031