2013-01-04 19 views
0

我的表單爲可變長度集合的每個成員生成一個字段,併爲輸入字段分配集合成員標識的ID。即,以Rails形式提取變量名稱參數?

<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 

但我發現了在量=參數一個零值誤差col.id。我怎樣才能訪問這些變量?

編輯我只是改變它,所以我使用javascript生成KV對哈希數組,並將其粘貼到一個隱藏的字段,所以我的控制器可以評估。它的工作原理,但從安全的角度來看,這有多可怕?

回答

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 
+0

其實,沒關係,這是相同的錯誤:/有沒有更好的方法來做到這一點? – user1436111

+0

更新了答案 – shweta

0

,而不是使用 「每個」 嘗試:

amount = collectors.map(&:id) 
+0

,它只會產生一組數量值,對嗎?我如何知道每個ID與哪個關聯? – user1436111

+0

我不確定我完全理解你的表單輸出應該是什麼。你能詳細說明一下嗎?你如何生成輸入字段? – neon