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’ %>
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.
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?
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
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’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.
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
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?
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).
On Sunday 09 April 2006 23:43, James McCarthy wrote:
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.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.