Rails cocoon-gem double nested forms “undefined method”

痴心易碎 提交于 2019-12-24 14:49:55

问题


I'm using the cocoon gem in my rails app and trying to double-nest forms.

The first "link_to_add_association" adds the valve_fields partial which contains another "link_to_add_association" it gives this error:

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

I haven't done anything differently for the first link than the second but for some reason it won't work

//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 

回答1:


According to your rfq_params method, you are having line_items_attributes inside valve_attributes. It means line_items are nested inside of valve, so in your form you should change this line f.simple_fields_for :line_items do |line_item| to valve.simple_fields_for :line_items do |line_item|.



来源:https://stackoverflow.com/questions/31252660/rails-cocoon-gem-double-nested-forms-undefined-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!