2011-06-23 17 views
3

我有一個嵌套的模型稱爲類別,我創建了一個嵌套的表單,允許在一種形式中創建一個類別和子類別。使用awesome_nested_set動態添加額外的孩子

這工作得很好,如果你在你的新方法,預先構建的孩子,像這樣:

class CategoriesController < InheritedResources::Base 
    def new 
    @category = Category.new 
    @category.children.build 
    end 
end 

的問題開始出現,當你想使用AJAX的形式動態地添加新的兒童。

這裏是我的形式:

%table 
    = form_for @category do |f| 
     %tr 
      %td= f.label :name 
      %td= f.text_field :name 

     %tr 
      %td(colspan=2) 
       %b Sub categories 

     - @category.children.each do |sub| 
      = f.fields_for :children, sub do |child| 
       = render "child_fields", :f => child 
     %tr 
      %td= link_to_add_fields "Add sub category", f, :children 
     %tr 
      %td= f.submit 'Save' 

這裏是我的link_to_add_fields助手方法(按照Ryans Railscast):

module ApplicationHelper 
    def link_to_add_fields(name, f, association) 
    new_object = f.object.class.reflect_on_association(association).klass.new 

    fields = f.fields_for(association, new_object, :child_index => "new_#{association}") do |builder| 
     render(:partial => association.to_s.singularize + "_fields", :locals => { :f => builder}) 
    end 
    link_to_function(name, "add_fields(this, '#{association}', '#{escape_javascript(fields)}')") 
    end 
end 

這裏是JavaScript它在

function add_fields(link, association, content) { 
    // Generate new unique index, so base this off the current time. 
    var new_id = new Date().getTime(); 
    var regexp = new RegExp("new_" + association, "g") 

    // Replace new_association with the current time.  
    $(link).closest("tr").before(content.replace(regexp, new_id)); 
} 

我注意到在預建的孩子們的渲染輸出是這樣的:

<input type="text" size="30" name="category[children_attributes][0][name]" id="category_children_attributes_0_name"> 

凡作爲Ajax生成的字段有:

<input type="text" size="30" name="category[children_attributes][1308801890744][name]" id="category_children_attributes_1308801890744_name"> 

這看起來是正確的,但是當我去點擊創建只有一個預建的孩子被保存。

UPDATE1 如果我把我的高清創建和類型PARAMS我只看到我的預建類沒有額外的一個調試器行我動態添加。

(rdb:4) params 
{"utf8"=>"✓", "authenticity_token"=>"iwq1Vx3jOZZsjd79Nj+qKNXxOwWP40c8XDFS8ooGMdg=", "category"=>{"name"=>"1", "children_attributes"=>{"0"=>{"name"=>"2"}}}, "commit"=>"Save", "action"=>"create", "controller"=>"categories"} 
+0

嗨,我只是想問你是否已經完成了多層次的awesome_nested_set與子類別嵌套。當我添加一個鏈接到'= f.link_to_add「在這個類別中添加一個子類別」:children'在child_fields部分時,我們可以添加多級子類別,但是當表單被提交時它只發送根級別並且葉級別類別到參數'「category」=> {「name」=>「one」,「children_attributes」=> {「new_1316937262917」=> {「name」=>「three」,「_destroy」= >「false」}}}'''它跳過子名稱爲'two'的子類別的子參數。我設置了一個 - >兩個 - >三個的形式。 – Zeeshan

+0

我用這與Ryan Bates nested_form gem。 – Zeeshan

回答

1

這是瀏覽器(至少Firefox)在窗體位於表格內時表現不尋常的結果。最簡單/最快的解決方法是將表格放入表格中。這是一個單行改變你的意見/分類/ new.haml文件:

= form_for @category do |f| 
    %table 
     %tr 

我如何調試它,萬一它有助於: 我首先檢查request.raw_post;參數不在那裏意味着鐵軌甚至沒有看到正確的請求。這指出了瀏覽器呈現問題。

我能夠通過螢火蟲調試問題,注意到窗體在渲染你的原始haml時會關閉笨拙。將表格移出表格似乎可以在Firefox中修復它。

我建議堅持div,主要是因爲它避免了很多奇怪的瀏覽器問題。

+0

我知道這會是簡單的事情!感謝那。除了我的表格之外,我使用div的一切,現在是解決問題的時候了。 – map7

+0

嗨map7,感謝您接受答案。我可以請你也給我獎勵這個問題的賞金嗎?這是接受答案的另一個步驟。答案左側應該有一個+50按鈕(儘管我不確定,因爲我以前從未做過賞金)。謝謝! – MakeSomething

+0

嗨,我只是想問你是否用awesome_nested_set爲子類進行了多級嵌套。當我添加一個鏈接到'= f.link_to_add「在這個類別中添加一個子類別」:children'在child_fields部分時,我們可以添加多級子類別,但是當表單被提交時它只發送根級別並且葉級別類別到參數'「category」=> {「name」=>「one」,「children_attributes」=> {「new_1316937262917」=> {「name」=>「three」,「_destroy」= >「false」}}}'''它跳過子名稱爲'two'的子類別的子參數。我設置了一個 - >兩個 - >三個的形式。 – Zeeshan