Rails嵌套的属性会破坏补丁,无法正常工作

时间:2019-04-16 19:36:19

标签: ruby-on-rails ruby ruby-on-rails-5 nested-attributes destroy

在更新我的协议模型时,我可能需要将1更新为许多附件,这可能涉及删除附件。我已经在模型上设置了rails nested属性,并且一切似乎都在为之工作,例如添加和更新。但我似乎无法删除工作。

我已经阅读了文档,并添加了所有必要的代码来启用此功能。但似乎没有任何坚持。

使用Rails 5.0.7.1和ruby 2.4.2

here is my agreement model 

  has_many :attachments,
           :class_name => 'Attachment',
           :foreign_key => 'attachable_id'

  accepts_nested_attributes_for :services
  accepts_nested_attributes_for :attachments, allow_destroy: true
here is the relevant controller code

  def update
    agreement = Contract::Agreement.find(params[:id])

    if agreement.update_attributes(agreement_params)
      render json: agreement, serializer: Contract::Agreement::Agreement
    else
      render json: {}, status: :bad_request
    end
  end

def agreement_params
    byebug
    unless params[:agreement][:attachments].blank?

      params[:agreement][:attachments_attributes] = params[:agreement][:attachments].map { |attachment|

        new_attachment = {}

        attachment.each do |key, value|
          new_attachment[:key] = value
        end
      }
      params[:agreement].delete :attachments
    end

    # byebug

    unless params[:agreement][:services].blank?
      params[:agreement][:services_attributes] = params[:agreement][:services]
      params[:agreement].delete :services
    end

    params.require(:agreement).permit(
      :status,
      :start_date,
      :end_date,
      :description,
      :parent,
      :level_id,
      :term_id,
      :service_provider_id,
      attachments_attributes: [
        :id,
        :file,
        :file_name,
        :file_size,
        :file_type,
        :attachable_type,
        :attachable_id,
        :_destroy
      ],
      services_attributes: [
        :id,
        :currency,
        :price,
        :facguid,
        :service_type_id,
        :agreement_id
      ]
    )
  end
and here is the test

it 'successfully deletes attachments', :attachments_testes => true do
        agreement = FactoryBot.create(:contract_agreement)

        attachment = FactoryBot.create(:agreement_attachment)
        attachment.attachable_id = agreement.id
        attachment.save

        expect(Attachment.count).to eq(1)

        agreement_attachments = [{id: attachment.id, _destroy: '1'}]
        patch :update, params: {id: agreement.id, agreement: {
            status: 'X',
            attachments: agreement_attachments
        }
        }
        agreement_response = JSON.parse(response.body)

        expect(response).to be_success
        expect(agreement_response['status']).to eq('X')
        byebug
        expect(Attachment.count).to eq(0)
      end

显然,附件应该从我的数据库中删除。当我在测试过程中对附件进行初步检查时,数据库中确实有1个附件,并且该附件链接到协议。但是,在响应成功返回后的最后一次期望中,所有内容都已更新,但附件仍然存在,就好像它忽略了_destroy。

1 个答案:

答案 0 :(得分:0)

在测试中,您具有:

params: { id: agreement.id, 
         agreement: { status: 'X', 
                      attachments: agreement_attachments } 
        }

应该不是:

params: { id: agreement.id, 
          agreement: { status: 'X',
                       attachments_attributes: agreement_attachments }
        }