0

私のフォームは、可変長コレクションのメンバーごとにフィールドを生成し、入力フィールドにコレクション メンバー ID の ID を割り当てます。すなわち、

<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">

コントローラーでは、次のようにコレクションを反復処理してこれらの値を取得しようとしています。

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

しかし、amount = params[col.id] で nil 値エラーが発生しています。これらの変数にアクセスするにはどうすればよいですか?

EDIT私はそれを変更したので、JavaScriptを使用してKVペアのハッシュの配列を生成し、隠しフィールドに貼り付けて、コントローラーがそれを評価できるようにします。それは機能しますが、セキュリティの観点からすると、これはどれほどひどいことでしょうか?

4

2 に答える 2

1

交換

<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">

<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">

コントローラーで

collectors.each do |col|
  amount = params[col.id] if params[col.id]
  # ...process this value
end
于 2013-01-04T06:17:06.247 に答える
0

「各」を使用する代わりに、次を試してください。

amount = collectors.map(&:id)
于 2013-01-04T07:20:36.130 に答える