使用cocoon,我的has_many通过关联不会更新或删除

时间:2015-10-03 11:14:18

标签: ruby-on-rails nested-forms cocoon-gem

使用cocoon通过关联为has_many构建我的嵌套表单。创建关联,但不会更新或删除关联。以下是我的嵌套表格:

edit.html.erb:

<div class="case_contacts-edit-container">
<%= link_to_add_association 'ADD CONTACT', f, :case_contacts, :"data-association-insertion-node" => "tbody#case_contacts_", :"data-association-insertion-method" => "append", class: 'action-button insure pull-right' %>
<table id="info-table3">
  <tbody id="case_contacts_">
  <tr>
    <td class="right">
      <strong>Name</strong>
    </td>
    <td class="right">
      <strong>Role</strong>
    </td>
    <td class="right">
      <strong>Note</strong>
    </td>
    <td>
    </td>
  </tr>
    <%= f.simple_fields_for :case_contacts do |case_contact| %>
      <%= render 'case_contact_fields', :f => case_contact %>
    <% end %>
  </tbody>
</table>

_case_contact_fields.html.erb:

<tr class="nested-fields">
      <td>
          <%= f.association :contact, :collection => (Contact.all.select(:first_name, :last_name, :email, :company_name, :id).map { |contact| [contact.name_with_company, contact.id] }), :prompt => "Add Contact", :class => 'table-input', :label => false %>
      </td>
      <td>
          <%= f.input :role, collection: Contact::TYPES, :prompt => "Add Role", :class => 'table-input', :label => false %>
      </td>
      <td>
          <%= f.input :note, :prompt => "Add Note", :class => 'table-input', :label => false %>
      </td>
          <%= f.hidden_field :firm_id, :value => @firm.id %>
      <td class="insurance-icon" style="text-align: center;">
          <%= link_to_remove_association f, class: 'dark-small remove_case_contacts' do %><span id="remove" class="glyphicon glyphicon-trash" original-title="Remove Contact"></span><% end %>
      </td>
  </tr>

cases_controller.rb(更新方法):

@case.assign_attributes(case_params)
@case.save

cases_controller.rb(允许的参数):

def case_params
  params.require(:case).permit(:name, :case_number, :docket_number, :description, :case_type, :subtype,
    :court, :county, :corporation, :status, :hearing_date, :filed_suit_date, :fee_agreement,
    :case_contacts_attributes => [:id, :case_id, :updated_at, :created_at, :firm_id, :role, :contact_id, :note, :_destroy])
end

case.rb(model):

has_many :case_contacts, :dependent => :destroy
  has_many :contacts, :through => :case_contacts
  accepts_nested_attributes_for :case_contacts, :reject_if => :all_blank, :allow_destroy => :true

contact.rb(型号):

  has_many :case_contacts, :dependent => :destroy
  has_and_belongs_to_many :cases, :join_table => 'case_contacts'

case_contact.rb(木匠模型):

belongs_to :affair, class_name: 'Case', :foreign_key => 'case_id'
belongs_to :contact

0 个答案:

没有答案