0

Pesky form error markup in Rails

Have you ever spent hours coding up the most awesome form you’ve ever seen only to have Rails break it with it’s error markup? I have, and it really sucks. So today I set out on a mission to solve this ever annoying problem. Here’s how I did it.

Step 1. Find the source.

If you’ve ever gone looking through Rails source, you know this wasn’t a very easy thing to do. But after some digging I found what was causing me so much grief.

# active_record_helper.rb in action_pack/action_view/helpers
@@field_error_proc = Proc.new{ |html_tag, instance| "<div class=\"fieldWithErrors\">#{html_tag}</div>" }

Step 2. Figure out what you want.

I found that the problem was that it was using a div to wrap the incorrect fields. This didn’t work for me. I needed a span so it wouldn’t break the layout. So what do you need? Maybe a div works, but you want to use a different class name? Whatever it is, figure it out. Firebug may be of some help to you.

Step 3. Patch Rails.

I love how easy this step is. Put a file in RAILS_ROOT/config/initializers called rails_ext.rb. You can really name this file whatever you want, but that’s what mine is called.

Next add these lines.

module ActionView
  class Base
    @@field_error_proc = Proc.new{ |html_tag, instance| "<span class=\"fieldWithErrors\">#{html_tag}</span>" }
    cattr_accessor :field_error_proc
  end
end

Now restart your server and enjoy.

Closing Thoughts.

Now I know what you’re thinking. Why don’t I just edit the css? Well here it is. My markup is a big deal. If I want a span I should have a span. I guess it really comes down to preference. Take it or leave it.

0

Leveraging the power of inheritance in Rails.

Here’s a neat trick.  It’s pretty simple but really helpful.

Problem: You’re always setting up the same stuff in every controller. Layouts, helper methods and so on.

Solution: Set up your own controller to inherit from.

Throw this into a new file in the controllers folder called base_controller.rb.

class BaseController < ApplicationController
 
  layout "base"
  helper :my_cool_helper  

end

Now take any controller and change the first line like below. Now anything in BaseController you get for free. So in the example below, PostsController’s layout will be “base” and will have the MyCoolHelper included also.

class PostsController < BaseController

  def list
  end

end

Wow, that was easy. Now it’s much easier to make (and change) default behaviors in your app. Hope this helps you, it sure helped me.

3

Git Documentation for Apple Dictionary

A couple months ago I found Priit Haamer’s blog post about the Ruby Dictionary for Mac OS X. So now I’ve ported the Git Documentation to Dictionary.app also. Please let me know of any bugs.  Enjoy!

Download

  1. Download gitdictionary file (<1MB, works only with 10.5 Leopard) and proceed to installation instructions.
Installation

  1. Unzip downloaded file into ~/Library/Dictionaries folder if you want to keep this dictionary only for yourself. You may need to create this folder if you haven’t installed any dictionaries before.
  2. To make the searching with Spotlight work, open Dictionary.app preferences pane and drag “Git” from the dictionaries list from bottom to the top because only the first one in this list will be searched from Spotlight.
  3. To make it available for all users on your computer, drop the Git.dictionary folder into /Library/Dictionaries folder.