Hi, I''m trying to do a bulk update on an array with the helper text_field. I''m trying to create an inputfield for every object in the array, but I''m not getting it right... example: class ClassA @arrayB[] end HTML (code with error): <% 0.upto(9) do |index| %> <%= text_field ''ClassA'', ''arrayB[index]'' %> non working alternative: <%= text_field ''ClassA[arrayB][]'', ''index'' %> Thanks in advance, Bas -- Posted via http://www.ruby-forum.com/.
On Saturday 08 April 2006 22:50, Bas wrote:> Hi, > > I''m trying to do a bulk update on an array with the helper text_field. > I''m trying to create an inputfield for every object in the array, but > I''m not getting it right... > > example: > > class ClassA > @arrayB[] > end > > HTML (code with error): > > <% 0.upto(9) do |index| %> > <%= text_field ''ClassA'', ''arrayB[index]'' %> > > non working alternative: > <%= text_field ''ClassA[arrayB][]'', ''index'' %> > > Thanks in advance, > Bas >I have a similair construction, but using hashes. I make a text field with the text_field_tag method. For example: field_value = hash_name[object.id.to_s] text_field_tag("hash_name[#{object.id}]", field_value) I create and fill the hash_name in the controller, putting all the objects using their id.to_s as key. Using string as key is necessary because the id''s will be passed as form parameters, which are text. Putting the commands to create the fields can be put in helpers. Hope this helps.
Thanks for the reply Wiebe, I''m able to create a form with all the objects from the Array/Hash having their own input with your construction, but there is no update when I accept the changes in my form. So I''m still looking for way to make this work. Any idea? -- Posted via http://www.ruby-forum.com/.
On Sunday 09 April 2006 00:30, Bas wrote:> Thanks for the reply Wiebe, > > I''m able to create a form with all the objects from the Array/Hash > having their own input with your construction, but there is no update > when I accept the changes in my form. > > So I''m still looking for way to make this work. > Any idea? >When using my technique, you of course have to take care of updating every entry in the hash yourself. In your update and/or create method (or create a method which contains shared code between create and update), so something like hash.each_key |id| do object = Object.find(id) object.name = hash_name[id] object.save end This is only an example, but it gives an idea. If anybody knows a way of automating this kind of mass-editing and saving, I''m all ears, but to my knowlege, that doesn''t exist. The text_field method for example, only takes the arguments useful for editing one object at a time. There is no text_fields_from_hash(collection, key, value) or whatever.
There is always the :index option in text_field this is what it is there for :) James -- Posted via http://www.ruby-forum.com/.
On Sunday 09 April 2006 13:49, James McCarthy wrote:> > There is always the :index option in text_field this is what it is there > for :) > > James >Hey, I never knew that. I should have read the general help for FormHelper. One question BTW. It says that this: <%= text_field "person", "name", "index" => 1 %> becomes <input type="text" id="person_1_name" name="person[1][name]" value="<%@person.name %>" /> But it would only be useful, it the value would instead become "@person[1].name", because else you cant get any values in the fields when you load the form. Or am I missing something?
Wiebe Cazemier wrote:> On Sunday 09 April 2006 13:49, James McCarthy wrote: > >> >> There is always the :index option in text_field this is what it is there >> for :) >> >> James >> > > Hey, I never knew that. I should have read the general help for > FormHelper. > > One question BTW. It says that this: > > <%= text_field "person", "name", "index" => 1 %> > > becomes > > <input type="text" id="person_1_name" name="person[1][name]" value="<%> @person.name %>" /> > > But it would only be useful, it the value would instead become > "@person[1].name", because else you cant get any values in the fields > when you > load the form. Or am I missing something?I would render a partial and pass a collection... render :partial => "my_partial", :collection => @persons and create the partial _my_partial.rhtml; <%= text_field :person, :name, :index => my_partial %> ---------------- passing the collection makes the partial iterate over it, for each iteration, each iteration creates a local of the current @person with the same name as the partial (minus the leading underscore). -- Posted via http://www.ruby-forum.com/.
On Sunday 09 April 2006 23:43, James McCarthy wrote:> I would render a partial and pass a collection... > > render :partial => "my_partial", :collection => @persons > > and create the partial _my_partial.rhtml; > > <%= text_field :person, :name, :index => my_partial %> > > ---------------- > passing the collection makes the partial iterate over it, for each > iteration, each iteration creates a local of the current @person with > the same name as the partial (minus the leading underscore).I see where you''re going. My situation is a bit different than this, so I''m not sure I can implement it like this, but I will definitely try to the next time I have to do something like this, because it would appear to be a good technique.