Not Beautiful Code
Posted by Michael Mon, 20 Mar 2006 10:36:00 GMT
There is an interesting piece of Ajax magic in Ruby on Rails called the “In place editor”. It’s implemented in just a few lines of code (shown here in the now classic “rails blog example”):
# Controller
class BlogController < ApplicationController
in_place_edit_for :post, :title
end
# View
< %= in_place_editor_field :post, title %>
This creates a really nice for field where you can click on the value and edit it in place. Cool huh?
Of course, there is really annoying catch. It bypasses the validation functions of rails. So you can edit in place, but it breaks a major (and very important) function of the framework. To quote David Heinemeier Hansson, “this is not beautiful code”. In fact it’s downright ugly and I’m disappointed that it’s even in the main distribution of Rails at this point. It’s even documented as breaking validations.
To make matters worse, the “fix” is to implement your own method for the in_place_edit function and basically reimplement validations in the controller
WHAT?
Ruby on Rails touts itself as a framework to “Make happy programmers”. This is ugly and should be viewed as the sort of thing that does not belong in the stable release of Rails.
