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

Jason Lee jlsync at gmail.com
Tue Apr 10 05:51:52 PDT 2012


Nested inside your form you can do something like the following,

- @widget.dookickies.sorted.each do |doohickey|
  = f.fields_for :doohickies, doohickey do |dform|
    = dform.text_field :title




On Tue, Apr 10, 2012 at 1:43 PM, Chris Adams <mail at chrisadams.me.uk> wrote:

> 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,
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lrug.org/pipermail/chat-lrug.org/attachments/20120410/4c91b262/attachment.html>


More information about the Chat mailing list