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

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

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?


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.

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

Rails would then output form fields with this setup:


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