Saturday, April 19, 2008

specifying rails input text size

I was bothered recently when building some forms by the default size attribute of 30 that the rails text_field form helper method adds by default to all text fields if one is not specified. More times then not, I don't want a size attribute specified, unless I explicitly provide one. Otherwise, my desired behaviour is browser default text box, which will then be presented with style.

Rails, makes this simple; just override the default values in the DEFAULT_FIELD_OPTIONS constant.
I did this by adding the following to my application_settings.rb (I've gotten in the habit of putting a lot of my constant definitions here instead of cluttering up environment files with constants)
ActionView::Helpers::InstanceTag::DEFAULT_FIELD_OPTIONS = { }

This works because the InstanceTag class in the form_helper.rb file checks to see if DEFAULT_FIELD_OPTIONS has been defined before it defines it for you. Opening up form_helper.rb shows me that the only defaults in this constant are { 'size" => 30 } so, I'm good just to set it to an empty hash in this case.

1 comment:

  1. I revised my approach on this here: