# \[noob\] referencing array of fields in form?

**URL:** <https://www.ruby-forum.com/t/noob-referencing-array-of-fields-in-form/111495>\
**Category:** Rails\
**Created:** [August 7, 2007, 10:57pm UTC](https://www.ruby-forum.com/t/noob-referencing-array-of-fields-in-form/111495 "2007-08-07T22:57:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rogboone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e0b2c6/32.png) [@rogboone](https://www.ruby-forum.com/u/rogboone)\
**Post date:** [August 7, 2007, 10:57pm UTC](https://www.ruby-forum.com/t/noob-referencing-array-of-fields-in-form/111495/1 "2007-08-07T22:57:57Z")

</div>

Hi All,

Noob question – but perhaps a combo of Ruby and Rails noobness.

## I have a table of “scores” and there are 150 “most recent” scores per user. I can retrieve those scores and display them in a form for the user. The user can update any and all scores. I’m now trying to implement the database “update” functionality (this is my first attempt at form processing). I’ve read through a few examples of using multiple models in forms and learned that I need to uniquely identify each field, which I’m able to do (I think).

\<% form\_for :score, :url =\> ‘/users/update\_scores’ do |form| %\>  
\<% for score in @scores %\>  
\<% fields\_for “scores[#{score.element.id}]”, score do |f| %\>

\<%= f.text\_field :rating, :size =\> 2 %\>  
\<%= score.element.description %\>  
\<% end %\>  
\<% end %\>  
\<% end %\>

This gives me HTML that looks like:

Question for this score Question for this score Question for this score
## … all the way up to name=“scores[150][rating]”

I’m confused about how to reference each text field back in my  
controller.  
I’ve tried params[:scores] and params[:scores][rating] along with  
many other attempts that I can’t remember now. I’ve been getting  
“nil” errors, or various syntax errors. Perhaps I’m using fields\_for  
incorrectly?

My overall goal is to compare each field in the submitted form to  
it’s original value (by requerying the database?) and only record new  
score records for fields that have changed (I need to keep the old  
records in the table so I can compare how the scores are changing over  
time). If the  
user only changes the rating for the 2nd question, I record a new record  
only for that change and timestamp it.

Thanks in advance for your time.  
Rog

---

<div class="post-metadata">

**Author:** ![rogboone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e0b2c6/32.png) [@rogboone](https://www.ruby-forum.com/u/rogboone)\
**Post date:** [August 8, 2007, 1:04am UTC](https://www.ruby-forum.com/t/noob-referencing-array-of-fields-in-form/111495/2 "2007-08-08T01:04:20Z")

</div>

Hi Roggie,

Roggie B. wrote:

> I’m confused about how to reference each text field back in my  
> controller.  
> I’ve tried params[:scores] and params[:scores][rating] along with  
> many other attempts that I can’t remember now.

To see what’s been returned in your params hash, you can use  
params.inspect.  
Sometimes I get lazy and just put ‘flunk’ as the first line in the  
method  
the form’s submitted to.

HTH,  
Bill

---

<div class="post-metadata">

**Author:** ![rogboone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e0b2c6/32.png) [@rogboone](https://www.ruby-forum.com/u/rogboone)\
**Post date:** [August 8, 2007, 3:07am UTC](https://www.ruby-forum.com/t/noob-referencing-array-of-fields-in-form/111495/3 "2007-08-08T03:07:33Z")

</div>

Bill W. wrote:

> To see what’s been returned in your params hash, you can use params.inspect.  
> Sometimes I get lazy and just put ‘flunk’ as the first line in the method  
> the form’s submitted to.
> 
> HTH,  
> Bill

I also strongly recommend using (as was recommended to me when I started  
out) LiveHTTPHeaders, an extension for Firefox that will show you what  
was POSTed to the application.

Cheers,  
Mohit.  
8/8/2007 | 9:06 AM.
