[noob] referencing array of fields in form?

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

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

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.