# Final Opinion :: Need final opinions on the best way to handle arrays in forms

**URL:** <https://www.ruby-forum.com/t/final-opinion-need-final-opinions-on-the-best-way-to-handle-arrays-in-forms/143305>\
**Category:** Rails\
**Created:** [July 17, 2008, 3:48am UTC](https://www.ruby-forum.com/t/final-opinion-need-final-opinions-on-the-best-way-to-handle-arrays-in-forms/143305 "2008-07-17T03:48:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![validkeys](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/da6949/32.png) [@validkeys](https://www.ruby-forum.com/u/validkeys)\
**Post date:** [July 17, 2008, 3:48am UTC](https://www.ruby-forum.com/t/final-opinion-need-final-opinions-on-the-best-way-to-handle-arrays-in-forms/143305/1 "2008-07-17T03:48:02Z")

</div>

Hi There, rails noob just trying to suss a few things out.

So I have 2 tables. 1 called Posts and the other called comments. On  
one page I need to be able to edit both the information about the post  
and the attributes of each comment for that post. So here is what the  
array might look like:

(Inside the params hash)

“post” =\> [  
{“id”=\>28, “title” =\> “My Title!”}  
]

“comments” =\> [  
{“id” =\> 1, “title” =\> “My Comment”, “body” =\> “My Body”},  
{“id” =\> 2, “title” =\> “My Comment”, “body” =\> “My Body”},  
{“id” =\> 3, “title” =\> “My Comment”, “body” =\> “My Body”}  
]

Now, in PHP i would name the fields something like this:

I was able to do this in rails as well using:

text\_field “comment[][title]”

… Then I read somewhere else that people were doing something like  
this

text\_field “comment\_1[title]”

and then parsing out the data.

Which one is better? I would tend to think that the array method above  
is superior, but then again, I may have no clue what I am talking  
about. Is there another method that I am not thinking of?

Thanks!!!

---

<div class="post-metadata">

**Author:** ![validkeys](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/da6949/32.png) [@validkeys](https://www.ruby-forum.com/u/validkeys)\
**Post date:** [July 17, 2008, 10:41am UTC](https://www.ruby-forum.com/t/final-opinion-need-final-opinions-on-the-best-way-to-handle-arrays-in-forms/143305/2 "2008-07-17T10:41:07Z")

</div>

> text\_field “comment\_1[title]”

No 1 would give you params[:comment][“0”][title]  
(I’m 100% not sure, but I think that Rails would make the index a  
string in any case,  
since it does with most things it gets from the browser)

No 2 would give you params[:comment\_1][title]

I like No 1, since it differentiates the name from the index,  
but it’s no big difference. So I guess, you can do what gives  
you the better looking code.

---

<div class="post-metadata">

**Author:** ![validkeys](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/da6949/32.png) [@validkeys](https://www.ruby-forum.com/u/validkeys)\
**Post date:** [July 17, 2008, 1:42pm UTC](https://www.ruby-forum.com/t/final-opinion-need-final-opinions-on-the-best-way-to-handle-arrays-in-forms/143305/3 "2008-07-17T13:42:08Z")

</div>

I had seen another possible option in railscasts where Ryan B. uses  
fields\_for. He would therefore create the form element without an  
array in the name and the fields\_for tag would be named something like  
post[comments][].

Rails would then output form fields with this setup:

post[comments][0][title]

It looks a lot cleaner and I think does the same thing.
