dw4050
dw4050

Reputation: 125

Receiving undefined method `reflect_on_association' for NilClass:Class Cocoon Rails 6

I'm currently using a double nested partial to add child records to a deeply nested form. The form inputs show, but the link_to_add_association button throws an exception. How can I reach the child form input with my link_to_add_association button?

providers_controller.rb

class ProviderContractsController < ApplicationController
  before_action :set_provider_contract, only: %i[show edit update destroy]

def new
    @provider_contract = ProviderForm.find(params[:provider_form_id])
    @provider_contract = ProviderContract.new(provider_form_id: @provider.id)
    @provider_contract.build_contract_term
    @provider_contract.contract_term.contract_term_fees.build


  rescue ActiveRecord::RecordNotFound
    redirect_to home_path, notice: 'The provider has been removed or has not been selected'
  end

  private

  # Use callbacks to share common setup or constraints between actions.
  def set_provider_contract
    @provider_contract = ProviderContract.find(params[:id])
  end

  # Never trust parameters from the scary internet, only allow the white list through.
  def provider_contract_params
    params.require(:provider_contract).permit(ProviderContract.attribute_names.map(&:to_sym),

                                              contract_term_attributes: ContractTerm.attribute_names.map(&:to_sym).push( contract_term_fees_attributes: ContractTermFee.attribute_names.map(&:to_sym).push(:id, :_destroy))
  end
end

_form.html.erb

 <%= simple_form_for(@provider_contract, html: { class: "ui form"}) do |f| %>
    <%= f.error_notification %>
    <%= f.error_notification message: f.object.errors[:base].to_sentence if f.object.errors[:base].present? %>

  <div class="ui tab" data-tab="stage-4">
      <%= f.simple_fields_for :contract_term do |ct| %>
        <%= render 'provider_contracts/partials/contract_term', f: ct %>
      <% end %>
    </div>
<% end %>

_contract_term.html.erb

<div id="#term_fees">
    <%= f.simple_fields_for contract_term_fees do |ff| %>
      <%= render 'provider_contracts/contract_term_fee_fields', f: ff %>
    <% end %>
  </div>
  <div class="pt-2 pb-2 links">
    <%= link_to_add_association f, :contract_term_fees, class: 'ui positive basic button' do %>
      Add Fees
    <% end %>
  </div>

contract_term.rb

class ContractTerm < ApplicationRecord
  belongs_to :provider_contract
  has_many :contract_term_fees, dependent: :destroy


  accepts_nested_attributes_for :contract_term_fees, reject_if: :all_blank, allow_destroy: true
end

Server Logs

ActionView::Template::Error (undefined method `reflect_on_association' for NilClass:Class):
    62:     <% end %>
    63:   </div>
    64:   <div class="pt-2 links">
    65:     <%= link_to_add_association  f, :contract_term_fees, class: 'ui secondary basic button' do %>
    66:       Add Fees
    67:     <% end %>
    68:   </div>

Upvotes: 0

Views: 1117

Answers (1)

nathanvda
nathanvda

Reputation: 50057

In general when this error occurs, it means that either an accepts_nested_attributes_for is missing, or that somewhere we hand in a nil object.

From the code you show, either

  • the accepts_nested_attributes_for :contract_terms is missing in Contract ?
  • the f.simple_fields_for contract_term_fees is a typo and should read f.simple_fields_for :contract_term_fees (contract_term_fees is obviously nil)

Note in Rails 6 as in Rails 5, you will also have to declare the inverse_of: of a relation to make sure the nested relations will save correctly (as a belongs_to is by default required, and without the inverse_of rails has no way of knowing it was provided).

So

  belongs_to :provider_contract, inverse_of: :contract_terms  

Upvotes: 1

Related Questions