我有一大堆孩子'已保存的对象,我想创建一个父对象,通过“亲戚”链接到孩子们。模型。
这个对象通过亲戚给了我一对多。
要明确:用户访问父母'页面,点击创建父母,并显示一个表单,让他们为父母命名,并为这个父母添加最多四个孩子(通过创建亲戚'),这些'关系'也被命名 - 这是一个重要的部分。所以,我可以将这个关系命名为“步子”。或者例如“儿子”。
这是我到目前为止的代码:
class Kid < ActiveRecord::Base
has_many :relatives
has_many :parents, through: :relatives
end
class Parent < ActiveRecord::Base
has_many :relatives
has_many :kids, through: :relatives
accepts_nested_attributes_for :relatives,
:reject_if => lambda { |a| a[:content].blank? },
:allow_destroy => true
end
class Relative < ActiveRecord::Base
belongs_to :parent
belongs_to :kid
end
class ParentsController < ApplicationController
before_action :set_parent, only: [:show, :edit, :update, :destroy]
before_action :lookup_kids, only: [:new, :edit]
# GET /parents
# GET /parents.json
def index
@parents = Parent.all
end
# GET /parents/1
# GET /parents/1.json
def show
end
# GET /parents/new
def new
@parent = Parent.new
4.times { @parent.relatives.build }
end
# GET /parents/1/edit
def edit
end
# POST /parents
# POST /parents.json
def create
@parent = Parent.new(parent_params)
parent_params[:relatives_attributes].each do |k,r|
@parent.relatives.build(r.except(:_destroy))
end
respond_to do |format|
if @parent.save
format.html { redirect_to @parent, notice: 'Parent was successfully created.' }
format.json { render :show, status: :created, location: @parent }
else
format.html { render :new }
format.json { render json: @parent.errors, status: :unprocessable_entity }
end
end
end
# cut for brevity.
private
# Use callbacks to share common setup or constraints between actions.
def set_parent
@parent = Parent.find(params[:id])
end
def parent_params
params.require(:parent).permit(:name,
relatives_attributes: [:parent_id, :kid_id, :relationship, :_destroy])
end
def lookup_kids
@kids = Kid.all #for this nursery.
end
end
<%= form_for(@parent) do |f| %>
<% if @parent.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@parent.errors.count, "error") %> prohibited this parent from being saved:</h2>
<ul>
<% @parent.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br>
<%= f.text_field :name %>
</div>
<h4>Kids:</h4>
<%= f.fields_for :relatives do |r| %>
<%= r.label :kid %>
<%= r.collection_select :kid_id,
@kids, :id, :name, include_blank: true%>
<%= r.label :relationship %>
<%= r.text_field :relationship %>
<%= r.check_box :_destroy %>
<%= r.label :_destroy, "Remove" %>
<br/>
<% end %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
ActiveRecord::Schema.define(version: 20151030113634) do
create_table "kids", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "parents", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "relatives", force: :cascade do |t|
t.string "relationship"
t.integer "parent_id"
t.integer "kid_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "relatives", ["kid_id"], name: "index_relatives_on_kid_id"
add_index "relatives", ["parent_id"], name: "index_relatives_on_parent_id"
end
当我进入&#39;创建&#39;在父母控制器中,我可以看到正确的参数正在通过,但关系记录没有被保存。这会自动发生吗?
我已尝试循环遍历:relatives_attributes,但似乎无法使用&#39; build&#39;。
我是如何得到这些亲戚的?记录保存?
编辑:添加已发布的参数:
parent"=>{
"name"=>"Dad",
"relatives_attributes"=>{
"0"=>{"kid_id"=>"2", "relationship"=>"Son", "_destroy"=>"0"},
"1"=>{"kid_id"=>"", "relationship"=>"", "_destroy"=>"0"},
"2"=>{"kid_id"=>"", "relationship"=>"", "_destroy"=>"0"},
"3"=>{"kid_id"=>"", "relationship"=>"", "_destroy"=>"0"}}}
编辑:我已更新此内容以显示我的最新编辑内容 - 请注意&#39; parent_params [:relatives_attributes] .each do | k,r |&#39;在控制器中。这现在保存了孩子记录,但唯一的问题是,它还保存了空白的字段!所以我有亲戚关系。小孩记录的空值记录。如何停止保存空字段(或创建空的相对记录)?
答案 0 :(得分:3)
答案是建立亲戚的每个子记录,如下:
parent_params[:relatives_attributes].each do |k,r|
@parent.relatives.build(r.except(:_destroy))
end
在致电@ parent.save之前。
但是,我仍有问题摆脱空白记录。因此,如果有人对此问题有答案,请在此处发表评论 - 或者如果有更好或更传统的做法,请打我。在此处跟进问题:Why is this reject_if in my model not rejecting blank records?
答案 1 :(得分:2)
你几乎就在那里,根据你的表单提交方式,你很可能也需要在Relative关联类中使用accepts_nested_attribute_for:
class Relative
belongs_to :parent
accepts_nested_attributes_for :parent
belongs_to :kid
accepts_nested_attributes_for :kid
end
如果这不起作用,请提交传递给控制器的参数,我们可以相应调整