nathanvda / cocoon

Dynamic nested forms using jQuery made easy; works with formtastic, simple_form or default forms
http://github.com/nathanvda/cocoon
MIT License
3.08k stars 383 forks source link

undefined method `template' for #<ActiveRecord::Associations::CollectionProxy #611

Closed rgmoises closed 2 years ago

rgmoises commented 2 years ago

Hi Guys! Im a newbie in using cocoon. i would like to ask your help in my issue below. I am using collection_check_boxes in nested-form. I cannot get the value of operator_contact.operator_contact_details.template to pass in checked. Hope you can help me. thank you.

model OperatorContact

  class OperatorContact < ApplicationRecord
    has_many :operator_contact_details, inverse_of: :operator_contact, dependent: :destroy
    accepts_nested_attributes_for :operator_contact_details, reject_if: :all_blank, allow_destroy: true
  end

model OperatorContactDetail

class OperatorContactDetail < ApplicationRecord
  audited
  belongs_to :operator_contact

  serialize :template, Array
end

controller OperatorContactsController

class OperatorContactsController < ApplicationController
  before_action :set_operator_contact, only: %i[ show edit update destroy ]
  before_action :list_templates

  private
    def set_operator_contact
      @operator_contact = OperatorContact.find(params[:id])
      @operator_contact_details = @operator_contact.operator_contact_details
    end

    def list_templates
      @all_templates = Template.where(status: true)
    end
end

_form.html.erb

<div>
  <%= f.fields_for :operator_contact_details do |builder| %>
    <%= render "operator_contact_detail_fields", f: builder %>
   <% end %>
<div>
<br>
  <%= link_to_add_association "Add Email", f, :operator_contact_details %>

_operator_contact_detail_fields.html.erb

<div class="nested-fields">
  <div class="mb-3 row">
    <%= f.label :email, "Email Address", class: "col-sm-3 col-form-label" %><br />
    <div class="col-sm-9">
      <%= f.text_field :email, class: 'form-control', title: "Email address", maxlength: 150 %>
    </div>
  </div>

  <div class="mb-3 row">
  <%= f.label :template, "Templates", class: "col-sm-3 col-form-label" %><br />  
    <div class="col-sm-9">
      <%= f.collection_check_boxes :template, @all_templates, :id, :name,  checked: @operator_contact.operator_contact_details.template do |b|%>
        <%= b.check_box(class: "btn-check") %> <%= b.label(class: "btn btn-outline-primary") %>
      <% end %>
    </div>
  </div>
  <%= link_to_remove_association "Delete", f %>
</div>

Error that I'm getting when I view or edit the record. I have no issue in creating new record.

app/views/operator_contacts/_operator_contact_detail_fields.html.erb where line #15 raised:

undefined method `template' for #<ActiveRecord::Associations::CollectionProxy [#<OperatorContactDetail id: 20, email: "sdsadas", template: ["", "6", "5", "7", "9"], created_at: "2021-09-13 11:49:29.883380000 +0200", updated_at: "2021-09-13 12:08:17.304052000 +0200", operator_contact_id: 15>, #<OperatorContactDetail id: 19, email: "eeee", template: ["", "8", "4", "2"], created_at: "2021-09-13 11:49:29.874591000 +0200", updated_at: "2021-09-13 12:08:17.343726000 +0200", operator_contact_id: 15>, #<OperatorContactDetail id: 18, email: "eee", template: ["", "6", "3", "2"], created_at: "2021-09-13 11:48:21.387335000 +0200", updated_at: "2021-09-13 12:08:17.366999000 +0200", operator_contact_id: 15>]>
rgmoises commented 2 years ago

got the answer. f.object.template