0

My form generates a field for each member of a variable-length collection and assigns the input field the ID of the collection member ID. I.e.,

<input type="text" id="1" val="Value for collection member with id #1">
<input type="text" id="5" val="Value for collection member with id #5">

In the controller I'm trying to pick up these values by iterating over the collection like this:

  collectors.each do |col|
    amount = params[col.id]
    # ...process this value
  end

But I'm getting a nil value error at amount = params[col.id]. How can I access these variables?

EDIT I just changed it so I use javascript to generate a array of hashes of the KV pairs and stick it into a hidden field so my controller can evaluate that. It works but from a security perspective, how awful is this?

2 Answers 2

1

replace

<input type="text" id="1" val="Value for collection member with id #1">
<input type="text" id="5" val="Value for collection member with id #5">

with

<input type="text" id="1" name="1" val="Value for collection member with id #1">
<input type="text" id="5" name="5" val="Value for collection member with id #5">

In the controller

collectors.each do |col|
  amount = params[col.id] if params[col.id]
  # ...process this value
end
Sign up to request clarification or add additional context in comments.

1 Comment

Actually, nevermind, it's the same error :/ Is there a better way to do this?
0

instead of using "each" try:

amount = collectors.map(&:id)

2 Comments

that will just yield an array of amount values, right? How do I know which is associated with each id?
I'm not sure I fully understand what your form output is supposed to be. Could you elaborate on that? How are you generating the input fields?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.