Every Formbuilder i see does the same, building some rows/tables/div structure around the fields it is be called on.
f.text_field => tr td label input
In theory this sounds great, but as soon as one tries to make some real life use of it it breaks down:
- How do i handle I18N ?
- How can i change the label text from the default
- How do i put the checkbox in front of the label
- How can i output only a field/2 fields in one row
- …
Form builders that always output label+input are unsuited for everyday development!
So what else can we do ?
#app/helpers/application_helper.rb
class ActionView::Helpers::FormBuilder
def build_translated_label(content)
if content.class == Array
label(content[0],content[1])#label + text
elsif content.class == String
label('',content)#text
else content.class == Symbol
#uses gettext translation, feel free to insert your own...
label(content,_("#{object.class}|#{content.to_s.capitalize.gsub('_',' ')}"))#label + translation
end
end
def row(label,content)
@template.content_tag('div',build_translated_label(label) + content.to_s,:class=>'row')
end
end
Usage
f.row(_('Movie|Website')+'http://', f.text_field(:website))
f.row(:tags, f.text_field(:tag_list)+_('comma divided'))
If someone wants the R-specs, drop me a mail 🙂