[LRUG] Nested form ordering in a Rails app - how best to do this?

Chris Adams mail at chrisadams.me.uk
Tue Apr 10 05:43:17 PDT 2012


Hi chaps, 

I'm working with a Rails 3.2 app at the moment, and I'm building a nested form using the idiomatic `form_for` rails syntax, so the code looks a bit like:

    <%= form_for @widget do |f| %>

      <%= f.text_field :content %>

        <% fields_for :doohickey do | builder | %>

          <%= builder.text_field :title  %>
          <%= builder.text_field :amount  %>

        <% end %>

      <%= f.submit %>

    <% end %>

Now assume :widget has_many :doohickeys, but doohickeys have a number of other properties you might want to sort them by.

I'd like to have more control over the order of how the children on a given object are displayed in a form when it's first generated, so I can order the children by colour, for example, or something similar. 

A bit like:

    <%= form_for @widget do |f| %>

      <%= f.text_field :content %>

        <% (fields_for :doohickey).some_custom_ordering_or_filtering_scope do | builder | %>

          <%= builder.text_field :title  %>
          <%= builder.text_field :amount  %>

        <% end %>

      <%= f.submit %>

    <% end %>

Anyone know if there are any libraries that let me make selective queries like this for displaying in a nested form? 

I haven't been working with rails long enough to be know how Rails 3 handles nested models in forms too well, yet, and right now my only options seem to be resort to using javascript in the client to rearrange the dom, or veering away from the handy `form_for` syntax that I've got used to everywhere else in app.

Suggestions?

Thanks,

Chris






-- 
Chris Adams
mobile: 07974 368 229
twitter: @mrchrisadams
www: chrisadams.me.uk
Sent with Sparrow



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lrug.org/pipermail/chat-lrug.org/attachments/20120410/45adaf37/attachment-0003.html>


More information about the Chat mailing list