无法使用“ accepts_nested_attributes_for”创建关联/嵌套的子记录

时间:2019-03-29 23:03:57

标签: ruby-on-rails activerecord

我在尝试使用“ accepts_nested_attributes_for”通过父级创建关联的子记录时遇到以下错误:

  

ActiveRecord :: RecordInvalid(验证失败:调查问题详细信息调查不能为空,调查问题详细信息活动详细信息必须存在):

想知道我是否缺少用于自动创建子记录的任何内容吗?

示例输入:(以下输入中的前4个键在保存之前合并到params哈希中)

  

{:organization_id =>“硬编码”,:activity_library_type => 0,:created_by =>“硬编码”,:updated_by =>“硬编码”,“ activity_name” =>“ ss-28”,“描述” => “”,“ content_path_url” =>“”,“ activity_status” => 0,“ activity_type” => 4,“ group” =>“ group”,“ code_level” =>“ code”,“ topic” =>“ topic “,”解决方案“ =>”解决方案“,” survey_question_details_attributes“ => [{” question“ =>” Question1“,” order“ => 1,” question_type“ => 0}]}}

参数:

  

params.require(:activity_detail).permit(:activity_name,:description,   :content_path_url,:activity_status,:activity_type,:group,:code_level,:topic,:solution,:duration_seconds,:survey_question_details_attributes => [:id,:question,:order,:question_type,:_ destroy])

下面是控制器和模型(每个活动都包含许多问题):

class ActivityController < ApplicationController
def create
    # Merged additional_attributes hash to content_params recieved from front end (first 4 keys mentioned above in sample input)
    full_activity_details_record = additional_attributes.merge(content_params)
    response = ActivityDetail.create(full_activity_details_record)
  end
end

class ActivityDetail < ApplicationRecord
  has_many :survey_question_details, dependent: :destroy, foreign_key: :survey_id
  accepts_nested_attributes_for :survey_question_details

  validates :activity_name, :organization_id,
            :activity_library_type, :activity_status, :activity_type,
            :created_by, :updated_by, presence: true
  validates :activity_name, uniqueness: { case_sensitive: false }, length: { maximum: 55 }
  validates :description, length: { maximum: 160 }

end


class SurveyQuestionDetail < ApplicationRecord
  belongs_to :activity_detail, foreign_key: :survey_id

  validates :question, :survey_id, :question_type, :order, presence: true
  default_scope { order(:order) }
end

2 个答案:

答案 0 :(得分:0)

由于您还没有分享您的观点,所以让我开始讨论。

首先,您已经验证了模型属性的存在状态为true,因此您将特别收到验证错误。

####in SurveyQuestionDetail model
###this is causing the error => Survey question details SURVEY can't be blank
##you must either pass the survey_id as hidden field or comment it out
validates :question, :survey_id, :question_type, :order, presence: true

更多,在您看来,您必须对表单中的嵌套属性使用fields_for

类似

<%= form_with(@activity_detail: show, local: true) do |form| %>
  # Show name and label
  <%= form.fields_for :survey_question_detail do |s| %>
    <%= s.label :number %>
    <%= s.number_field :number %>
    ##can also include hidden fields to add other model...survey_id
  <% end %>
  # Submit Button
<% end %>

希望这会有所帮助。

答案 1 :(得分:0)

我认为创建的参数不完整。参数应放在:activity_detail键下

:activity_detail => {
  {
    :organization_id=>"hardcoded", 
    :activity_library_type=>0, 
    :created_by=>"hardcoded", 
    :updated_by=>"hardcoded", 
    "activity_name"=>"s-s-28", 
    "description"=>"", 
    "content_path_url"=>"", 
    "activity_status"=>0, 
    "activity_type"=>4, 
    "group"=>"group", 
    "code_level"=>"code", 
    "topic"=>"topic", 
    "solution"=>"solution", 
    "survey_question_details_attributes"=>[
      {
        "question"=>"Question1", 
        "order"=>1, 
        "question_type"=>0
      }
    ]
  }
}