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

Chris Adams mail at chrisadams.me.uk
Tue Apr 10 06:15:29 PDT 2012


Thanks Jason, that did it!

C

On 10 April 2012 13:51, Jason Lee <jlsync at gmail.com> wrote:
>
> 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,
>
>
> _______________________________________________
> Chat mailing list
> Chat at lists.lrug.org
> http://lists.lrug.org/listinfo.cgi/chat-lrug.org
>



-- 
Chris Adams
mob: 07974 368 229
tel: 0207 558 8971
skype: chris.d.adams
twitter: mrchrisadams



More information about the Chat mailing list