Re: [rails-oceania] "Add more" in nested forms
I'm doing similar at the moment with Ryan Bates nested_form - may be worth investigating https://github.com/ryanb/nested_form.git On Wed, Sep 28, 2011 at 8:20 AM, markbrown4 wrote: > Hi, > > Here's what I'm trying to achieve: > > > -- > <%= @review.title %> > <%= form_for @review do |review_form| %> > > <%= review_form.fields_for(:sections) do |section_form| %> > ><%= section_form.label :title, 'Section' %> ><%= section_form.text_field :title %> > > > ><%= section_form.fields_for(:items) do |item_form| %> > > <%= item_form.label :title, 'Item' %> > <%= item_form.text_field :title %> > ><% end %> ><%= link_to 'Add item', > new_section_item_path(section_form.object) %> > > > <% end %> > <%= review_form.submit %> > > <% end %> > > <%= link_to 'Add section', new_section_path %> > -- > class Review < ActiveRecord::Base > has_many :sections > > accepts_nested_attributes_for :sections > end > -- > class Section < ActiveRecord::Base > has_many :items > belongs_to :section > > accepts_nested_attributes_for :items > end > -- > class Item < ActiveRecord::Base > belongs_to :section > end > -- > How would you achieve adding of new items & sections to the form and > keep track of what items need to be edited and which items need to be > created when you save? > > I kept new items separate initially and iterated over > params[:new_items] to do the creates .e.g > > > This fell down when we tried to implement autosave with ajax though as > the the created items didn't have the id's in the form and the page > wasn't being refreshed, is it best to keep the new items separate or > to create first and populate the form with real id's so the automatic > mappings work? > > My other question is about FormBuilder and partials. > If I had an item partial and wanted to create an item from a request > and return the HTML for the item, how do I create the section_form > FormBuilder that the partial requires? > > -- > <%= section_form.fields_for(item) do |item_form| %> > > <%= item_form.label :title, 'Item' %> > <%= item_form.text_field :title %> > > <% end %> > -- > > Thanks! > > -- > You received this message because you are subscribed to the Google Groups > "Ruby or Rails Oceania" group. > To post to this group, send email to rails-oceania@googlegroups.com. > To unsubscribe from this group, send email to > rails-oceania+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/rails-oceania?hl=en. > > -- You received this message because you are subscribed to the Google Groups "Ruby or Rails Oceania" group. To post to this group, send email to rails-oceania@googlegroups.com. To unsubscribe from this group, send email to rails-oceania+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rails-oceania?hl=en.
Re: [rails-oceania] "Add more" in nested forms
Hi Mark, Let me start with your second question first since it is infinitely easier to answer. If you are going to have a form partial depend on different form build builders then you will need to pass the form builder into the partial as a local. That way the parent forms can pass their form builder into the partial and the partial can call fields_for on it. As for your first question, there are so many ways to skin this cat… Let me ask first. Do you really want to nest your collection forms 2 deep? I can't imagine a good user experience coming from this. Probably my opinion on UX is not what you were after here ;-) so, if you do want to nest them and you don't want to do full page posts, you will need to provide some Javacript facility for cloning the child form and incrementing the indexes for the field names, ids and labels. If you want to go down this road I can post some Javascript snippets that will get you a fair way down the road. You will also need a hidden field in each child form to hold the id of items that have already been saved. If the id already exists Active record will be able to work out that they are not new records (provided they are in the datastore). Cheers, Adam On 28/09/2011, at 8:23 AM, markbrown4 wrote: > Couldn't post an image for some reason.. > > http://im g841.image shack.us/im g841/2681/nest.png > > without spaces. > > -- > You received this message because you are subscribed to the Google Groups > "Ruby or Rails Oceania" group. > To post to this group, send email to rails-oceania@googlegroups.com. > To unsubscribe from this group, send email to > rails-oceania+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/rails-oceania?hl=en. > -- You received this message because you are subscribed to the Google Groups "Ruby or Rails Oceania" group. To post to this group, send email to rails-oceania@googlegroups.com. To unsubscribe from this group, send email to rails-oceania+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rails-oceania?hl=en.
[rails-oceania] "Add more" in nested forms
Couldn't post an image for some reason.. http://im g841.image shack.us/im g841/2681/nest.png without spaces. -- You received this message because you are subscribed to the Google Groups "Ruby or Rails Oceania" group. To post to this group, send email to rails-oceania@googlegroups.com. To unsubscribe from this group, send email to rails-oceania+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rails-oceania?hl=en.
[rails-oceania] "Add more" in nested forms
Hi, Here's what I'm trying to achieve: -- <%= @review.title %> <%= form_for @review do |review_form| %> <%= review_form.fields_for(:sections) do |section_form| %> <%= section_form.label :title, 'Section' %> <%= section_form.text_field :title %> <%= section_form.fields_for(:items) do |item_form| %> <%= item_form.label :title, 'Item' %> <%= item_form.text_field :title %> <% end %> <%= link_to 'Add item', new_section_item_path(section_form.object) %> <% end %> <%= review_form.submit %> <% end %> <%= link_to 'Add section', new_section_path %> -- class Review < ActiveRecord::Base has_many :sections accepts_nested_attributes_for :sections end -- class Section < ActiveRecord::Base has_many :items belongs_to :section accepts_nested_attributes_for :items end -- class Item < ActiveRecord::Base belongs_to :section end -- How would you achieve adding of new items & sections to the form and keep track of what items need to be edited and which items need to be created when you save? I kept new items separate initially and iterated over params[:new_items] to do the creates .e.g This fell down when we tried to implement autosave with ajax though as the the created items didn't have the id's in the form and the page wasn't being refreshed, is it best to keep the new items separate or to create first and populate the form with real id's so the automatic mappings work? My other question is about FormBuilder and partials. If I had an item partial and wanted to create an item from a request and return the HTML for the item, how do I create the section_form FormBuilder that the partial requires? -- <%= section_form.fields_for(item) do |item_form| %> <%= item_form.label :title, 'Item' %> <%= item_form.text_field :title %> <% end %> -- Thanks! -- You received this message because you are subscribed to the Google Groups "Ruby or Rails Oceania" group. To post to this group, send email to rails-oceania@googlegroups.com. To unsubscribe from this group, send email to rails-oceania+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rails-oceania?hl=en.