Skip to content

Simpler FormBuilder

Nick Sutterer edited this page Jul 29, 2014 · 3 revisions

The idea is to fork/wrap simple_form or rewrite Rails' FormBuilder, adopt all the goodness for a quick form rendering implementation, and remove all the ugliness.

Fields_for

collection :songs do
  property :title
end

When rendering this with #fields_for, we get a weird over-complicated form that results in an incoming hash. The form builder tries to prefix each collection item with an id, and so on.

{
  album: {
    songs: {
      1234 => {title: "I Don't Know"},
      5678 => {title: "Meant To Be"}
    }
  }
}

This hash has nothing to do with its representation in a JSON document. Why do forms and JSON API hashes differ in Rails? It doesn't make sense and overcomplicates the task of processing those.

What we really want is a simple array that gets processed by Reform "the natural way". Also, the prefixing doesn't help. When POSTing to /albums/1, I already know that I'm posting to an album, so why would I use that stupid prefix?

This would be processable by Reform and representable without any hacks, and also represents the natural way you'd encode this data structure in JSON.

songs: [
  {title: "I Don't Know"},
  {title: "Meant To Be"}
]

Property names

{
  album_attributes: {
  }
}

The _attributes suffix is an ugly hack from Rails, we don't need it in Reform. Again, this leads to a diverging JSON and form representation.

Date Picker

release_date(1i)

This should be a hash like

release_date: {"1i": ..}