Rails cocoon-gem 双嵌套形式 "undefined method"

Rails cocoon-gem double nested forms "undefined method"

我在我的 rails 应用程序中使用 cocoon gem 并尝试双重嵌套表单。

第一个 "link_to_add_association" 添加了包含另一个 "link_to_add_association" 的 valve_fields 部分,它给出了这个错误:

ActionView::Template::Error (undefined method `fetch_value' for nil:NilClass):
21:                         .line_items
22:                                 =f.simple_fields_for :line_items do |line_item|
23:                                         =render 'line_item_fields', :f => line_item
24:                 //error line//  =link_to_add_association "add line", f, :line_items

第一个 link 与第二个 link 我没有做任何不同的事情,但由于某些原因它不起作用

//Rfq controller 
def rfq_params
  params.require(:rfq).permit(
    :quote_number,:rep_id, :owner, :customer_id, :customer_name, :end_user_list, :application_list, :due, :ship_date, :is_budgetary, :notes, :mandatory_due_date, 
    valves_attributes: [:id, :_destroy, :productline, :discwedge, :valvetype, :valveends, :valvematerial, :valveconfig, :valvenotes,
    line_items_attributes: [
    :tag, :qty, :size, :class, :operator, :trim, :_destroy
    ]
    ])
end


//form partial:

  .valves
    =f.fields_for :valves do |valve|
      =render 'valve_fields', :f => valve
    =link_to_add_association 'add valve', f, :valves, "data-association-insertion-traversal" => "next"

//valve partial
.line_items
    =f.simple_fields_for :line_items do |line_item|
        =render 'line_item_fields', :f => line_item
    =link_to_add_association "add line", f, :line_items 

根据您的 rfq_params 方法,您在 valve_attributes 中有 line_items_attributes。这意味着 line_itemsvalve 内是 nested,因此在您的表单中,您应该将此行 f.simple_fields_for :line_items do |line_item| 更改为 valve.simple_fields_for :line_items do |line_item|