2013-08-30 35 views
0

我正在尋找一種方法來爲has_many關聯創建表單,以便我可以動態設置將會去那裏的對象的數量。在動態創建對象的Rails中嵌套表格

到目前爲止,我看到的例子,當你用空白對象初始化的關聯,就像這樣:

2.times { @office.employees.build } 

所以,你可以使用fields_for,或什麼之後,和它的作品。在像我這樣的情況下,我不知道究竟會增加多少員工(甚至可能爲零)。

他們是否有關於如何使用,將動態創建如哈希形式結束了任何簡單的解決辦法:

{...,「僱員」 => {「0」 => {。 ...},「1」=> {...}}

在此先感謝。

回答

0

請設置的nessted_form寶石使用link

= simple_nested_form_for @ffice do |f| 
    = f.fields_for :employees do |employee|  
     = render 'employee_fields', :f => employee 
    .links 
    = image_tag('plus.png', :class => 'plus-link')  
    = f.link_to_add 'Add another employee', :employees 

link_to_add」這個輔助方法在nessted_form定義,有助於增加了許多員工的形式。

我希望這會有所幫助。