问题:
我在提交表单时收到一条错误消息,上面写着:
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
控制器:
我的new
andcreate
方法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中找到的那些,但似乎仍然无法修复此错误。如果调试此问题需要任何其他信息,请告诉我。谢谢!