Re: Trouble with multiple fields

Steve Prentice
Sun, 23 Oct 2005 17:07:07 -0000

Wouldn''t this work:

i = 0
@people.each do |person|
	<text_field "person", "name", "index" =>
i++>
end


On Sun, 2005-10-23 at 11:51 -0400, Mike DeGraw-Bertsch
wrote:
> Howdy,
>
> Reading about the "index" option for checkboxes and text fields,
I
> understand that it will create values like this:
>
> <text_field "person", "name", "index"
=> 1>
>
> becomes
>
> <input type="text" id="person_1_name"
name="person[1][name]" value="<%
> = @person.name %>"/>
>
> However, shouldn''t it really evaluate to this instead:
>
> <input type="text" id="person_1_name"
name="person[1][name]" value="<%
> = @person[1].name %>"/>
>
> I''ve been losing my mind over this.  In my program, @person is a
hash,
> so @person[1].name resolves very nicely, but @person.name simply
> disappears.  Do I write a horrible hack that says "@ person.name >
@person[1].name" before each field is printed?  That just seems so
> ugly.  I''m hoping there''s an easy, obvious way that
I''m missing.
>
> Another thought I had is extending FormHelper to do this for me.
> Looking at its code, it seems my problem lies here:
>
>       def object
>         @template_object.instance_variable_get "@#{@object_name}"
>       end
>
> Would I want to change object to be something like:
>
>       def object(*index)
>         if (!index)
>           return @template_object.instance_variable_get
> "@#{@object_name}"
>         else
>           return @template_object.instance_variable_get
> "@#{@object_name}[#{index}]"
>         end
>       end
>
> Hoping I missed something really obvious, or that the above extension
> isn''t lunatic.
>
>   -Mike
>
> _______________________________________________
> Rails mailing list
> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> http://lists.rubyonrails.org/mailman/listinfo/rails