I have 3 tables: Addresses (id, name, place) AddressContacts (address_id, contact, addrescontype_id) AddressConTypes (id, contype) Models: Address (has_many :addresscontacts) AddressContact (belongs_to :address; belongs_to :addresscontype) AddressConType (has_many :addresscontact) I made a AddressController + view to list all addresses + contacts. Now the problem: I want to be able to update the addesses+contacts on the same page and in the same form. Is this possible? The edit page looks like this: -------- <%= start_form_tag :action => "update" %> <%= hidden_field "address", "id" %> <p><label for="address_name">Naam</label><br/> <%= input(''address'', ''name'') %> </p> <p><label for="address_place">Plaats</label><br/> <%= input(''address'', ''place'') %> </p> <% @address.addresscontacts.each do |@addresscontact| %> <p> <%= collection_select(''addresscontact'', ''addresscontype_id'', @addresscontypes, ''id'', ''contype'') %> <%= input(''addresscontact'', ''contact'') %> </p> <% end %> </p> <input type="submit" value="Update" /> <%= end_form_tag %> -------- But I can''t update the addresscontacts like this. Do I really have to split this up? I want to keep it as simple as possible for the users. Thanks for any tips or links, Roeland
Here''s the devel version if that helps: http://roelandmoors.be/adressen/ It''s a little different from the example, but I hope you now what I mean. On Tue, Dec 28, 2004 at 09:38:11PM +0100, Roeland Moors wrote:> I have 3 tables: > Addresses (id, name, place) > AddressContacts (address_id, contact, addrescontype_id) > AddressConTypes (id, contype) > > Models: > Address (has_many :addresscontacts) > AddressContact (belongs_to :address; belongs_to :addresscontype) > AddressConType (has_many :addresscontact) > > I made a AddressController + view to list all addresses + contacts. > Now the problem: > I want to be able to update the addesses+contacts on the same > page and in the same form. Is this possible? > > The edit page looks like this: > -------- > <%= start_form_tag :action => "update" %> > <%= hidden_field "address", "id" %> > <p><label for="address_name">Naam</label><br/> > <%= input(''address'', ''name'') %> > </p> > <p><label for="address_place">Plaats</label><br/> > <%= input(''address'', ''place'') %> > </p> > <% @address.addresscontacts.each do |@addresscontact| %> > <p> > <%= collection_select(''addresscontact'', ''addresscontype_id'', > @addresscontypes, ''id'', ''contype'') %> > <%= input(''addresscontact'', ''contact'') %> > </p> > <% end %> > </p> > > <input type="submit" value="Update" /> > <%= end_form_tag %> > -------- > > But I can''t update the addresscontacts like this. > Do I really have to split this up? > I want to keep it as simple as possible for the users. > > Thanks for any tips or links, > > Roeland > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > >
Hi Roeland, You have multiple input fields with the same name: <input id="addresscontact_contact" name="addresscontact[contact]" size="30" type="text" value="blalba" /> and likewise for the select widgets. You have to add a unique identifier to these fields, like the id of the addresscontact: <input id="addresscontact_0079_contact" or something similar. Otherwise the form can''t possibly work. I''m not sure if the form helpers can do that but if not, you should be able to extend them pretty easily to cater this kind of situations. If you have the time and courage to extend the helpers, be sure to share your work and you''ll earn eternal fame in the community ;-) //jarkko On 28.12.2004, at 22:38, Roeland Moors wrote:> -------- > <%= start_form_tag :action => "update" %> > <%= hidden_field "address", "id" %> > <p><label for="address_name">Naam</label><br/> > <%= input(''address'', ''name'') %> > </p> > <p><label for="address_place">Plaats</label><br/> > <%= input(''address'', ''place'') %> > </p> > <% @address.addresscontacts.each do |@addresscontact| %> > <p> > <%= collection_select(''addresscontact'', ''addresscontype_id'', > @addresscontypes, ''id'', ''contype'') %> > <%= input(''addresscontact'', ''contact'') %> > </p> > <% end %> > </p> > > <input type="submit" value="Update" /> > <%= end_form_tag %> > -------- > > But I can''t update the addresscontacts like this. > Do I really have to split this up? > I want to keep it as simple as possible for the users. > > Thanks for any tips or links, > > Roeland > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Jarkko Laine http://jlaine.net _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
On Tue, Dec 28, 2004 at 10:56:58PM +0200, Jarkko Laine wrote:> Hi Roeland, > > You have multiple input fields with the same name: > <input id="addresscontact_contact" name="addresscontact[contact]" > size="30" type="text" value="blalba" /> > and likewise for the select widgets. > > You have to add a unique identifier to these fields, like the id of the > addresscontact: <input id="addresscontact_0079_contact" or something > similar. Otherwise the form can''t possibly work. I''m not sure if the > form helpers can do that but if not, you should be able to extend them > pretty easily to cater this kind of situations. If you have the time > and courage to extend the helpers, be sure to share your work and > you''ll earn eternal fame in the community ;-) >Maybe something like loop(@address, ''addresscontacts'') do |new_id| ... end The id must be recreatable in the controller in an easy way. I''ll think about it and try to create something.> > On 28.12.2004, at 22:38, Roeland Moors wrote: > > >-------- > ><%= start_form_tag :action => "update" %> > ><%= hidden_field "address", "id" %> > ><p><label for="address_name">Naam</label><br/> > > <%= input(''address'', ''name'') %> > ></p> > ><p><label for="address_place">Plaats</label><br/> > > <%= input(''address'', ''place'') %> > ></p> > ><% @address.addresscontacts.each do |@addresscontact| %> > > <p> > > <%= collection_select(''addresscontact'', ''addresscontype_id'', > > @addresscontypes, ''id'', ''contype'') %> > > <%= input(''addresscontact'', ''contact'') %> > > </p> > ><% end %> > ></p> > > > ><input type="submit" value="Update" /> > ><%= end_form_tag %> > >-------- > > > >But I can''t update the addresscontacts like this. > >Do I really have to split this up? > >I want to keep it as simple as possible for the users. > > > >Thanks for any tips or links, > > > >Roeland > >_______________________________________________ > >Rails mailing list > >Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > >http://lists.rubyonrails.org/mailman/listinfo/rails > > > -- > Jarkko Laine > http://jlaine.net> _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails
On Tue, Dec 28, 2004 at 10:56:58PM +0200, Jarkko Laine wrote:> Hi Roeland, > > You have multiple input fields with the same name: > <input id="addresscontact_contact" name="addresscontact[contact]" > size="30" type="text" value="blalba" /> > and likewise for the select widgets. > > You have to add a unique identifier to these fields, like the id of the > addresscontact: <input id="addresscontact_0079_contact" or something > similar. Otherwise the form can''t possibly work. I''m not sure if the > form helpers can do that but if not, you should be able to extend them > pretty easily to cater this kind of situations. If you have the time > and courage to extend the helpers, be sure to share your work and > you''ll earn eternal fame in the community ;-) >I managed to get it working. This is how it looks in the view: <% @address.addresscontacts.each do |contact| %> <p> <% object = "addresscontact_#{contact.id}" %> <% eval "@#{object} = contact" %> <%= hidden_field(object, "id") %> <%= collection_select(object, ''addresscontype_id'', @addresscontypes, ''id'', ''contype'') %> <%= input(object, ''contact'') %> </p> <% end %> and added this stuff in the controller: @address.addresscontacts.each do |contact| object = "addresscontact_#{contact.id}" @contact = Addresscontact.find(@params[object][''id'']) @contact.attributes = @params[object] @contact.save #TODO end Now I only have to beautify it.> //jarkko > > On 28.12.2004, at 22:38, Roeland Moors wrote: > > >-------- > ><%= start_form_tag :action => "update" %> > ><%= hidden_field "address", "id" %> > ><p><label for="address_name">Naam</label><br/> > > <%= input(''address'', ''name'') %> > ></p> > ><p><label for="address_place">Plaats</label><br/> > > <%= input(''address'', ''place'') %> > ></p> > ><% @address.addresscontacts.each do |@addresscontact| %> > > <p> > > <%= collection_select(''addresscontact'', ''addresscontype_id'', > > @addresscontypes, ''id'', ''contype'') %> > > <%= input(''addresscontact'', ''contact'') %> > > </p> > ><% end %> > ></p> > > > ><input type="submit" value="Update" /> > ><%= end_form_tag %> > >-------- > > > >But I can''t update the addresscontacts like this. > >Do I really have to split this up? > >I want to keep it as simple as possible for the users. > > > >Thanks for any tips or links, > > > >Roeland > >_______________________________________________ > >Rails mailing list > >Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > >http://lists.rubyonrails.org/mailman/listinfo/rails > > > -- > Jarkko Laine > http://jlaine.net> _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails
> I managed to get it working. > This is how it looks in the view: > <% @address.addresscontacts.each do |contact| %> > <p> > <% object = "addresscontact_#{contact.id}" %> > <% eval "@#{object} = contact" %> > <%= hidden_field(object, "id") %> > <%= collection_select(object, ''addresscontype_id'', > @addresscontypes, ''id'', ''contype'') %> > <%= input(object, ''contact'') %> > </p> > <% end %> > > and added this stuff in the controller: > @address.addresscontacts.each do |contact| > object = "addresscontact_#{contact.id}" > @contact = Addresscontact.find(@params[object][''id'']) > @contact.attributes = @params[object] > @contact.save #TODO > end > > Now I only have to beautify it.You might consider expanding on a proposal I wrote a while back to handle this automatically at the CGI layer. I haven''t had time to implement this unfortunately, but if you''re motivated it shouldn''t be so difficult. Hmm, after about 30 minutes of searching, I realized the post no longer exists! The crux of my suggestion would be to make a set of helpers (or modifications to the existing helpers) to encode either a primary key or a label with the name of the attribute, and then programmatically pluck it back out. It would also have the nice property of allowing you to associate 2 or more form fields with a single entity, and then have a bunch of those on a single page. *And* then you could do some business with dynamic forms using those fancy javascript dom techniques. DHH: Is there any way to retrieve the page that once lived at: http://actionpack.rubyonrails.org/show/FormBuildingByCollections I''m still considering implementing this myself, when I find time. Rewriting the proposal wouldn''t be terrible, it''d give me a new chance to rethink things, but it takes still more time... Regards, -- Dave Steinberg http://www.geekisp.com/ http://www.steinbergcomputing.com/