Posted almost 10 years ago. Visible to the public.

How to change the order of nested forms being rendered (especially blank forms)

Generally for nested forms, a blank form is placed below all other existing object forms. If you would like to change the position of the blank form(s) you can reorder the object's one-to-many association. For example you can put the blank form on top with the following snippet:

Copy
actors = movie.actors actors.build actors.unshift(actors.pop(1)) # won't work with Rails 4+

Because build_for_form creates new objects and appends them to the one-to-many association collection object you only have to reorder the collection objects.

Sorting with Rails 3+

Copy
= form.fields_for :children, @parent.children.sort_by(&:name) do |fieldsform| …
Growing Rails Applications in Practice
Check out our new e-book:
Learn to structure large Ruby on Rails codebases with the tools you already know and love.

Owner of this card:

Avatar
Martin Straub
Last edit:
about 5 years ago
by Martin Straub
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Martin Straub to makandra dev
This website uses short-lived cookies to improve usability.
Accept or learn more