I''m trying to create a form where a collection is created, using the example from the Rails 0.9.5 weblog posting: http://weblog.rubyonrails.com/archives/2005/01/26/rails-095-a-world-of- fixes-and-tweaks/ I''m having a bit of trouble. The controller currently looks like this: class StudentController < ApplicationController def new render_text @params.inspect end def create end end And the view: <form action="new" method="post"> <% (1..3).each do |i| %> <p> First Name: <%= text_field "student[]", "first_name" %> <br /> Last Name: <%= text_field "student[]", "last_name" %> </p> <% end %> <input type="submit"> </form> The idea is to have a form where I can enter information for three different students at the same time, and somehow create them all at once. Unfortunately, all of the first_name and last_name text fields have the index 4, like this: <p> First Name: <input id="student_4_first_name" name="student[4][first_name]" size="30" type="text" value="" /> <br /> Last Name: <input id="student_4_last_name" name="student[4][last_name]" size="30" type="text" value="" /> </p> This means that only one set of first names and last names is actually passed to the create method, whose output is this: {"action"=>"new", "controller"=>"student", "student"=>{"4"=>{"first_name"=>"a first", "last_name"=>"a last"}}} ("a first" and "a last" are the first and last names that I entered in the first set of fields.) What''s the "magic sauce" I need for this to work so that the text fields have different indices? Thanks, Guan
Guan, There are two ways to use the index field. The first is to have the index field be equal to the "id" field of your objects. This is useful for already existent data that needs an "update" form. For example: (Assuming @students contains a collection of "Student" ActiveRecord objects:) <% @students.each do |@student| %> <p> First Name: <%= text_field "student[]", "first_name" %> <br /> Last Name: <%= text_field "student[]", "last_name" %> </p> <% end %> The second way is to just use some number that uniquely identifies each set of form elements (which is probably what you''re trying to do, as in a "new/create" situation):> <% (1..3).each do |i| %> > <p> > First Name: <%= text_field "student", "first_name", "index" => i %> <br /> > Last Name: <%= text_field "student", "last_name", "index" => i %> > </p> > <% end %>Note that the square brackets are not used in this case. The square brackets are "magic markers" which are meant to trigger Rails to insert the object.id (student.id) value in between the brackets whenever they are found at the end of an object name passed in to a form helper method. Take care, Duane Johnson (canadaduane) On Sat, 5 Feb 2005 17:58:40 +0100, Guan Yang <guan-Tn7NQuJrROJAfugRpC6u6w@public.gmane.org> wrote:> I''m trying to create a form where a collection is created, using the > example from the Rails 0.9.5 weblog posting: > http://weblog.rubyonrails.com/archives/2005/01/26/rails-095-a-world-of- > fixes-and-tweaks/ > > I''m having a bit of trouble. The controller currently looks like this: > > class StudentController < ApplicationController > def new > render_text @params.inspect > end > > def create > end > end > > And the view: > > <form action="new" method="post"> > <% (1..3).each do |i| %> > <p> > First Name: <%= text_field "student[]", "first_name" %> <br /> > Last Name: <%= text_field "student[]", "last_name" %> > </p> > <% end %> > <input type="submit"> > </form> > > The idea is to have a form where I can enter information for three > different students at the same time, and somehow create them all at > once. Unfortunately, all of the first_name and last_name text fields > have the index 4, like this: > > <p> > First Name: <input id="student_4_first_name" > name="student[4][first_name]" size="30" type="text" value="" /> <br /> > Last Name: <input id="student_4_last_name" > name="student[4][last_name]" size="30" type="text" value="" /> > </p> > > This means that only one set of first names and last names is actually > passed to the create method, whose output is this: > > {"action"=>"new", "controller"=>"student", > "student"=>{"4"=>{"first_name"=>"a first", "last_name"=>"a last"}}} > > ("a first" and "a last" are the first and last names that I entered in > the first set of fields.) > > What''s the "magic sauce" I need for this to work so that the text > fields have different indices? > > Thanks, > Guan > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
On 5 Feb 2005, at 18:18, Duane Johnson wrote:>> <% (1..3).each do |i| %> >> <p> >> First Name: <%= text_field "student", "first_name", "index" => i >> %> <br /> >> Last Name: <%= text_field "student", "last_name", "index" => i >> %> >> </p> >> <% end %> > > Note that the square brackets are not used in this case. The square > brackets are "magic markers" which are meant to trigger Rails to > insert the object.id (student.id) value in between the brackets > whenever they are found at the end of an object name passed in to a > form helper method.Thanks, the "index" => i works fine on text_field, but it doesn''t work when I do a collection_select. In that case the field does not get an index. Is this a bug? Guan
Hmmm, sounds like a bug to me. Could you post that as a new ticket on dev.rubyonrails.com? In the meantime, you can use your own HTML and options_from_collection_for_select, like this: <select name="category[parent_id]"> <option value="0"><Main Category></option> <%= options_from_collection_for_select categories_in_dropdown, "id", "name", @default_category_id %> </select> -- Duane On Sat, 5 Feb 2005 18:32:48 +0100, Guan Yang <guan-Tn7NQuJrROJAfugRpC6u6w@public.gmane.org> wrote:> On 5 Feb 2005, at 18:18, Duane Johnson wrote: > >> <% (1..3).each do |i| %> > >> <p> > >> First Name: <%= text_field "student", "first_name", "index" => i > >> %> <br /> > >> Last Name: <%= text_field "student", "last_name", "index" => i > >> %> > >> </p> > >> <% end %> > > > > Note that the square brackets are not used in this case. The square > > brackets are "magic markers" which are meant to trigger Rails to > > insert the object.id (student.id) value in between the brackets > > whenever they are found at the end of an object name passed in to a > > form helper method. > > Thanks, the "index" => i works fine on text_field, but it doesn''t work > when I do a collection_select. In that case the field does not get an > index. Is this a bug? > > Guan > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Another possible workaround is like this: <%= collection_select ("category", nil, categories_in_dropdown, ''id'',''name'', {}, "index"=>"id", "multiple"=>''multiple'', "size"=>5) %> Note that in this case, I''ve made the collection be multiple selectable since that is what I infer is wanted by having the variable be indexed. The resulting html is a bit rough at the edges (since the variable name has extra underscores) but works well. Victor Duane Johnson wrote:> Hmmm, sounds like a bug to me. > > Could you post that as a new ticket on dev.rubyonrails.com? > > In the meantime, you can use your own HTML and > options_from_collection_for_select, like this: > > <select name="category[parent_id]"> > <option value="0"><Main Category></option> > <%= options_from_collection_for_select categories_in_dropdown, > "id", "name", @default_category_id %> > </select> > > > > -- Duane > > On Sat, 5 Feb 2005 18:32:48 +0100, Guan Yang <guan-Tn7NQuJrROJAfugRpC6u6w@public.gmane.org> wrote: > >>On 5 Feb 2005, at 18:18, Duane Johnson wrote: >> >>>><% (1..3).each do |i| %> >>>><p> >>>> First Name: <%= text_field "student", "first_name", "index" => i >>>>%> <br /> >>>> Last Name: <%= text_field "student", "last_name", "index" => i >>>>%> >>>></p> >>>><% end %> >>> >>>Note that the square brackets are not used in this case. The square >>>brackets are "magic markers" which are meant to trigger Rails to >>>insert the object.id (student.id) value in between the brackets >>>whenever they are found at the end of an object name passed in to a >>>form helper method. >> >>Thanks, the "index" => i works fine on text_field, but it doesn''t work >>when I do a collection_select. In that case the field does not get an >>index. Is this a bug? >> >>Guan >> >>_______________________________________________ >>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