【问题标题】:rails nested attributes destroy not working on patchrails嵌套属性破坏无法在补丁上工作
【发布时间】:2019-09-06 23:42:46
【问题描述】:

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

我已阅读文档并添加了所有必要的代码以启用此功能。但似乎没有任何问题。

使用 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。

【问题讨论】:

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


    【解决方案1】:

    在你的测试中你有:

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

    不应该是:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-04-28
      • 1970-01-01
      • 2015-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-30
      相关资源
      最近更新 更多