Rails 4嵌套表单不将子项链接到父项

时间:2015-03-04 19:00:01

标签: ruby-on-rails ruby-on-rails-4 parent-child nested-forms nested-attributes

我试图在类Participant和GroupParticipant之间创建一个嵌套的表单。但是,当我提交表单时,只有参与者保存在系统中。以下是模型,控制器,视图和输出的相关部分:

participant.rb

class Participant < ActiveRecord::Base
  #relationships
   has_many :group_participants#, inverse_of: :participant
   has_many :groups, through: :group_participants
   accepts_nested_attributes_for :group_participants

group_participant.rb

class GroupParticipant < ActiveRecord::Base
#relationships
belongs_to :participant#, inverse_of: :group_participant
belongs_to :group

participant_controller.rb

class ParticipantsController < ApplicationController
  before_action :set_participant, only: [:show, :edit, :update, :destroy]

  def new
    @participant = Participant.new
    @participant.group_participants.build
  end

  def create
    @participant = Participant.new(participant_params)
    respond_to do |format|
      if @participant.save
        format.html { redirect_to @participant, notice: 'Participant was successfully created.' }
        format.json { render :show, status: :created, location: @participant }
      else
        format.html { render :new }
        format.json { render json: @participant.errors, status: :unprocessable_entity }
      end
    end
  end

  def participant_params
    params.require(:participant).permit(:first_name, :last_name, :nickname, :female, :notes, :date_of_birth, :grade, :role, :active, :size,
    group_participants_attributes: [:id, :group_id, :participant_id, :active, :year])
  end

我正在使用simple_form和nested_form进行视图 参与者/ new.html.erb

<%= simple_nested_form_for @participant do |f| %>
  <%= f.input :role, label: "Role", collection: Participant::ROLES %>
  <%= f.input :first_name, label: 'First Name' %>
  <%= f.input :last_name, label: 'Last Name' %>
  <%= f.input :female, label: 'Gender', collection: Participant::FEMALE, required: true  %>

  <%= f.fields_for :group_participants do |gp| %>
    <%= gp.text_field :group_id %>
  <% end %>

  <%= f.button :submit %>
 <% end %>

当我提交表单时,只创建参与者,而不是GroupParticipant。我已经看到了使用正确的foreign_id自动创建子对象的示例,但是当查看rails服务器输出时,我看到Particip_id不包含在GroupParticipant参数中:

 Parameters: {"utf8"=>"✓", "authenticity_token"=>"...",
             "participant"=>{"role"=>"child", "first_name"=>"Bob", "last_name"=>"Belcher", "female"=>"false",
             "group_participants_attributes"=>{"0"=>{"group_id"=>"1"}}}, "commit"=>"Create Participant"}

提交表单后,服务器还仅输出SQL以创建参与者:

  BEGIN
  SQL (0.3ms)
    INSERT INTO "participants" ("created_at", "female", "first_name", "last_name", "role", "updated_at")
    VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id"  [ ["created_at", "2015-03-04 19:57:07.674319"], ["female", "f"], ["first_name", "Bob"], ["last_name", "Belcher"], ["role", "child"], ["updated_at", "2015-03-04 19:57:07.674319"]]
  (0.4ms)  COMMIT

我已经尝试了一切让它像这样工作,但我无法让它工作。我怀疑它与控制器中的构建调用有关,但我试图修改它无济于事。还尝试将“inverse_of”属性添加到模型中。

如果有人对我的问题有任何见解,包括对Rails4上的工作示例的引用,我将不胜感激。在这一点上,我也愿意在控制器中修改一些东西,比如首先保存父母然后将其id传递给孩子,但是现在我无法解决这个问题。

1 个答案:

答案 0 :(得分:0)

我认为改变这个可以解决你的问题:

ParticipantsController

def create
    @participant = Participant.new(participant_params)
    ...
    ...
end