Dec 27, 2011

More on Twitter Bootstrap in Rails


Yesterday I wrote about error handling in Rails while using Twitter Bootstrap.  One of the interesting things I was able to do with the custom form builder was move the active record attribute errors to the input fields they're associated with.  The example below is a bit simple but it should get the point across....




If you look at the template that created the form below you can see that that it doesn't include any special functionality.  All the work happens in the form builder.





If you look at the "text_field" method in the code below you can see that it just calls "input_wrapper" to add some html around the "text_field".  Since "input_wrapper" already had to test each active record attribute for errors so that it could add "error" to the classes of those elements it was pretty straight forward to tack an inline-help span on the end.





The code above is not elegant, it's just an experiment.  I certainly don't recommend using it as is but I wanted to share just in case some one is thinking about similar problems.


In general though I am fairly happy with the separation of concerns so far.  I think that by treating the multi-element fields bootstrap expects in it's forms as primitives I will be able keep my templates focused on the content and my presenters on the logic of the view.

1 comment:

Don Marges said...

Hey Michael,

Thanks for this! I was wondering if you have heard of SimpleForm? It does the job of using bootstrap with form validation very nicely.

Cheers!