olivier Hericord
2005-Aug-09 08:40 UTC
is it possible to create multiple records from one form and to get validation work ?
hi, imagine a simple contact table with just one field labeled name... nothing particular in the contact model just a simple validation like being sure the name is longer than 3 characteres in the scaffold generated view to create a new record we have <%= textfield ''contact'' ''name'' %> and it works great to create one record but, can i use <%= textfield ''contact'' ''name'', :index => 1 %> ?? if i have <%= textfield ''contact'' ''name'', :index => 1 %> <%= textfield ''contact'' ''name'', :index => 2 %> in my view for instance, what has to be the code in my controller to create these 2 contacts and is a validation on one phone will highlight the correct phone in the view?? thanks olivier _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Sean T Allen
2005-Aug-09 15:48 UTC
Re: is it possible to create multiple records from one form and to get validation work ?
i have what you are talking about working for CREATE but not UPDATE yet.... if you are willing to wade through some code I can send you the relevant templates and controller to look at... its a work in progress on my end so it''s ugly... let me know... olivier Hericord wrote:> hi, > > imagine a simple contact table with just one field labeled name... > nothing particular in the contact model just a simple validation like > being sure the name is longer than 3 characteres > > in the scaffold generated view to create a new record we have > <%= textfield ''contact'' ''name'' %> > and it works great to create one record > > but, can i use <%= textfield ''contact'' ''name'', :index => 1 %> ?? > > if i have <%= textfield ''contact'' ''name'', :index => 1 %> <%= textfield > ''contact'' ''name'', :index => 2 %> in my view for instance, what has to > be the code in my controller to create these 2 contacts and is a > validation on one phone will highlight the correct phone in the view?? > > thanks > > olivier > >------------------------------------------------------------------------ > >_______________________________________________ >Rails mailing list >Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >http://lists.rubyonrails.org/mailman/listinfo/rails > >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Zachery Hostens
2005-Aug-10 07:49 UTC
Re: is it possible to create multiple records from one form and to get validation work ?
olivier, <%= textfield ''contact'' ''name'', :index => 1 %> will give you params[:contact][1][:name] in your action. and so one for each index above. (:index => 1 MAY index to [0] but i dont think so) On 8/9/05, olivier Hericord <olivier.hericord.lists-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> hi, > > imagine a simple contact table with just one field labeled name... > nothing particular in the contact model just a simple validation like being > sure the name is longer than 3 characteres > > in the scaffold generated view to create a new record we have > <%= textfield ''contact'' ''name'' %> > and it works great to create one record > > but, can i use <%= textfield ''contact'' ''name'', :index => 1 %> ?? > > if i have <%= textfield ''contact'' ''name'', :index => 1 %> <%= textfield > ''contact'' ''name'', :index => 2 %> in my view for instance, what has to be the > code in my controller to create these 2 contacts and is a validation on one > phone will highlight the correct phone in the view?? > > thanks > > olivier > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >-- Zachery Hostens <zacheryph-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
olivier Hericord
2005-Aug-10 12:30 UTC
Re: is it possible to create multiple records from one form and to get validation work ?
it seems that it''s gonna be a little more complex than just writing <%= textfield ''contact'' ''name'', :index => 1 %>, <%= textfield ''contact'' ''name'', :index => 2 %> .... i''m writing a little helper right now....once it''s finish i will post them. but i think my problem is a very common one: each time A has_many B it''s difficult to create many B while creating a new A (without using sessions and using just one view) On 8/10/05, Zachery Hostens <zacheryph-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > olivier, > > <%= textfield ''contact'' ''name'', :index => 1 %> > > will give you params[:contact][1][:name] in your action. and so one > for each index above. (:index => 1 MAY index to [0] but i dont think > so) > > On 8/9/05, olivier Hericord <olivier.hericord.lists-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > hi, > > > > imagine a simple contact table with just one field labeled name... > > nothing particular in the contact model just a simple validation like > being > > sure the name is longer than 3 characteres > > > > in the scaffold generated view to create a new record we have > > <%= textfield ''contact'' ''name'' %> > > and it works great to create one record > > > > but, can i use <%= textfield ''contact'' ''name'', :index => 1 %> ?? > > > > if i have <%= textfield ''contact'' ''name'', :index => 1 %> <%= textfield > > ''contact'' ''name'', :index => 2 %> in my view for instance, what has to be > the > > code in my controller to create these 2 contacts and is a validation on > one > > phone will highlight the correct phone in the view?? > > > > thanks > > > > olivier > > > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > > > > > -- > Zachery Hostens <zacheryph-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Wilson
2005-Aug-10 13:42 UTC
Re: is it possible to create multiple records from one form and to get validation work ?
I recommend using the render(:partial, :collection) process for this. I have code like this, in my app: <%= render(:partial => ''member'', :collection => @members) %> (@members is an Array created in the controller.) The partial form looks like this: (_member.rhtml) <% @member = member %> # Here we''re assigning the local ''member'' variable created by the # render(:partial) to an instance variable, which allows us to use the # ''error_messages_for'' helper on it. <!--[form:member]--> <% if member.errors.count > 0 then -%> <%= error_messages_for(''member'', :id => "errorExplanation#{member_counter}") %> <% end -%> # If you don''t need separate error messages for each item in the collection, you probably don''t need that. I had to get a little trickier, since the form has to support a dynamic number of ''Member'' entries. Further down in the partial form, I have stuff like this: <%= text_field ''member'', ''first_name'', ''index'' => member_counter, ''maxlength'' => 15, ''size'' => 15, ''class'' => ''required'' %> (Note that the syntax for ''select'' with an index is a little funny:) <%= select ''member'', ''sex_code'', [[''Unknown'',''U''], [''Male'',''M''], [''Female'',''F'']], {}, {''index'' => member_counter, ''class'' => ''required''} %> Finally, here''s a snippet from the controller action that the form ''Submits'' to: if params[:member] then @members = Array.new params[:member].each do |key, mem| @members << Member.new(mem) end session[:current_members] = @members end The controller sees the form entries as a hash, with the same parameter name as the model instance. (e.g. <%= text_field ''blah'' %> maps to params[:blah]) Hopefully I haven''t misunderstood your question, and spammed the list with my crappy code. Heh. --Wilson. olivier Hericord wrote:> it seems that it''s gonna be a little more complex than just writing <%= > textfield ''contact'' ''name'', :index => 1 %>, <%= textfield ''contact'' > ''name'', :index => 2 %> .... > > i''m writing a little helper right now....once it''s finish i will post them. > > but i think my problem is a very common one: each time A has_many B it''s > difficult to create many B while creating a new A (without using > sessions and using just one view) > > On 8/10/05, *Zachery Hostens* <zacheryph-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org > <mailto:zacheryph-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>> wrote: > > olivier, > > <%= textfield ''contact'' ''name'', :index => 1 %> > > will give you params[:contact][1][:name] in your action. and so one > for each index above. (:index => 1 MAY index to [0] but i dont think > so) > > On 8/9/05, olivier Hericord <olivier.hericord.lists-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org > <mailto:olivier.hericord.lists-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>> wrote: > > hi, > > > > imagine a simple contact table with just one field labeled name... > > nothing particular in the contact model just a simple validation > like being > > sure the name is longer than 3 characteres > > > > in the scaffold generated view to create a new record we have > > <%= textfield ''contact'' ''name'' %> > > and it works great to create one record > > > > but, can i use <%= textfield ''contact'' ''name'', :index => 1 %> ?? > > > > if i have <%= textfield ''contact'' ''name'', :index => 1 %> <%> textfield > > ''contact'' ''name'', :index => 2 %> in my view for instance, what > has to be the > > code in my controller to create these 2 contacts and is a > validation on one > > phone will highlight the correct phone in the view?? > >