2012-06-01 88 views
0

型號軌通用fields_for - 強迫孩子指數

class Person < AR 

end 

控制器

def new 
    @people = [] 
    3.times {@people << Person.new) 
end 

查看

=form_tag :people_form do 
[email protected]_with_index do |person, i| 
    =fields_for :person, person do |person_fields| 
    =person_fields.text_field :name 

這是有點明顯,這將產生對每個人相同的HTML( name = people_form [person] [name])。有沒有一種方法可以讓它插入索引,因此每個人都是唯一的索引? (沒有javacript hack)。

例如這將是很好,如果我能得到它的產生是這樣的:

<input name='people_form[0][name]' /> 
<input name='people_form[1][name]' /> 
+0

沒有工作?讓我們知道 –

回答

0

簡單的比你想象的

=form_tag :people_form do 
[email protected]_with_index do |person, i| 
    =fields_for "people_form[#{i}]", person do |person_fields| 
    =person_fields.text_field :name 
+0

不,不是的。這會使用'people_form [people_form [0]] [name]'而不是'people_form [people_form] [0] [name]'創建輸入。 – jtompl