1

问题:

我在提交表单时收到一条错误消息,上面写着:

ActiveModel::MassAssignmentSecurity::AdmissionRecordsController#create 中的错误

无法批量分配受保护的属性:admission_record

我的设置:

我正在使用 Rails 3.2.3,还有额外的 gem,包括 Cocoon 1.0.14 和 Simple_Form 2.0.2

风景:

我的app/views/admission_records/_form.html.haml样子:

= simple_form_for [@admission, @record] do |f|
  = f.simple_fields_for :vital_signs, @record.vital_signs.build do |vs|
    = render :partial => "vital_sign_fields", :locals => { :f => vs }
    = link_to_add_association "Add Vital Signs", f, :vital_signs
  = f.submit

我的app/views/admission_records/_vital_sign_fields.html.haml样子:

.nested-fields
  = f.label :sbp
  = f.text_field :sbp
  ...
  = link_to_remove_association "Remove Vital Sign"

我基本上想做的是我有一个名为 AdmissionRecord 的资源嵌套在另一个名为 PatientAdmission 的资源中(route.rb如下所示)。我有另一个名为的资源VitalSign,我希望在创建AdmissionRecord

我的config/routes.rb文件看起来像:

resources :patient_admissions do
  resources :admission_records
end

模型:

我的app/models/patient_admission.rb样子:

class PatientAdmission < ActiveRecord::Base
  has_many :admission_records, :dependent => :destroy
end

我的app/models/admission_record.rb样子:

class AdmissionRecord < ActiveRecord::Base
  belongs_to :patient_admission
  has_many :vital_signs, :dependent => :destroy

  accepts_nested_attributes_for :vital_signs, :rejects_if => :all_blank, :allow_destroy => true
  attr_accessible :vital_signs_attributes
end

我的app/models/vital_sign.rb样子:

class VitalSign < ActiveRecord::Base
  belongs_to :admission_record
  attr_accessible # just fields that appear in the form
end

控制器:

我的newandcreate方法AdmissionRecordsController看起来像:

before_filter do
  @admission = PatientAdmission.find(params[:patient_admission_id])
end

def new
  @record = @admission.admission_records.build
end

def create
  @record = @admission.admission_records.build(params[:admission_record])
  @vital_sign = @record.vital_signs.build(params[:vital_signs])
  @vital_sign.save
  if @record.save
    # Flash success and redirect to the right place
  else
    # Flash error and render :new
  end
end

请求:

请帮我找出哪里出错了。我已经搜索了几个小时,并查看了演示应用程序的其他示例和源代码,例如在cocoon_simple_form_demo中找到的那些,但似乎仍然无法修复此错误。如果调试此问题需要任何其他信息,请告诉我。谢谢!

4

1 回答 1

0

好的,我刚刚遇到了这个问题,并通过在 belongs_to 模型中输入一行代码来解决它。

# patient_admission.rb
Class PatientAdmission < ActiveRecord::Base
  attr_accessible :admission_record_attributes

  accepts_nested_attributes_for :admission_record

  ...
end

这是另一个解决方案:)

于 2013-05-03T09:16:59.733 回答