Forum: Ruby on Rails text_field and arrays

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
740d332f3fd021646add1ab5f8b4dea2?d=identicon&s=25 Bas (Guest)
on 2006-04-08 22:50
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
A28088e0bbbdecd6663d554f7be1273f?d=identicon&s=25 Wiebe Cazemier (Guest)
on 2006-04-08 23:29
(Received via mailing list)
On Saturday 08 April 2006 22:50, Bas wrote:

> end
> 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.
740d332f3fd021646add1ab5f8b4dea2?d=identicon&s=25 Bas (Guest)
on 2006-04-09 00:30
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?
A28088e0bbbdecd6663d554f7be1273f?d=identicon&s=25 Wiebe Cazemier (Guest)
on 2006-04-09 00:43
(Received via mailing list)
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.
A025f68d8100c2f0fa78c6cfcc4cbfd1?d=identicon&s=25 James Mccarthy (noagenda)
on 2006-04-09 13:49
There is always the :index option in text_field this is what it is there
for :)

James
A28088e0bbbdecd6663d554f7be1273f?d=identicon&s=25 Wiebe Cazemier (Guest)
on 2006-04-09 16:48
(Received via mailing list)
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?
A025f68d8100c2f0fa78c6cfcc4cbfd1?d=identicon&s=25 James Mccarthy (noagenda)
on 2006-04-09 23:43
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).
A28088e0bbbdecd6663d554f7be1273f?d=identicon&s=25 Wiebe Cazemier (Guest)
on 2006-04-10 00:10
(Received via mailing list)
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 topic is locked and can not be replied to.