(Or, “Crazy forks everywhere”.)

All the way back in RailsCast 197 Ryan Bates introduced us to his javascript-powered dynamic nested form gem, nested_form. It’s pretty useful, and in concert with Rails’ existing accepts_nested_attributes_for :relation, got pretty close to solving a problem I haven’t ever seen cleanly solved without unwieldy controller code and custom javascript.

Ryan has previously kept it up to date (bless his socks), but now after a three months and a gradual accumulation of about twenty outstanding pull requests (some that fix niggles like hardcoded markup, the undocumented testing environment, and weird jumbled-attribute record creation bugs), I need to move on so I can get back to work.

Fortunately, elmatou forked nested_form, merged a bunch of changes and made plenty of his own, and put it all in a branch called ‘defacto’. These changes include:

  • Configurable wrapper markup,
  • Actual removal of ‘removed’ forms,
  • Positionable insertion of newly-added nested forms,
  • Storage of the field template in a javascript variable, not inside a hidden <div> (meaning your markup doesn’t break when using Formtastic).

Also fortunately, fxposter (a long-time nested_form committer) has also forked, and improved the testing situation by swapping out Appraisal with an integrated dummy app.

As is tradition for any moderately popular project, there are a ton more forks and the situation is as messy as hell, but if you need one of the above fixes sooner than later, then pull in one of these versions of nested_form by putting the following in your Gemfile:

# The Ryan Bates original:
#gem 'nested_form' # The Ryan Bates original

# Fxposter's fork:
gem 'nested_form', :git => 'git://github.com/fxposter/nested_form.git' 

# Elmatou's fork:
gem 'nested_form', :git => 'git://github.com/elmatou/nested_form.git', :branch => 'defacto'

I’ve elected to go with Elmatou’s fork (for the moment) for the configurable markup. Formtastic’s ordered <li> field setup is not friendly at all with Ryan’s hardcoded <div> method.

(tl;dr nested_form has some odd bugs, elmatou’s fork has less, but fxposter’s fork has a better testing setup. I’m using elmatou’s to get on with building stuff. It’s all kind of sad right now.)