嵌套的_destroy属性包含Mongoid和strong_parameters

时间:2014-12-20 20:09:21

标签: ruby-on-rails mongoid strong-parameters

我想使用请求属性销毁embeds_many关系中的文档。

class Experience
  include Mongoid::Document

  embeds_many :variations, class_name: 'ExperienceVariation'
  accepts_nested_attributes_for :variations, allow_destroy: true
end

class ExperiencesController < ApplicationController
  def update
    if @experience.update_attributes(experience_params)
      show
    else
      render json: @experience.andand.errors, status: :unprocessable_entity
    end
  end

  def experience_params
    params.require(:experience).permit(
      ...
      variations: [
        :id, :delete, ...
      ]
    )
  end
end

在使用_destroy发出请求时,出现错误:

Mongoid::Errors::UnknownAttribute - 
Problem:
  Attempted to set a value for '_destroy' which is not allowed on the model ExperienceVariation.

如何让嵌套_destroy工作?

1 个答案:

答案 0 :(得分:0)

Rails方面的一切都很好。请求发送variations而不是variations_attributes的是前端(AngularJS)。