ruby-on-rails – 如何从父控制器保存我的子记录?

前端之家收集整理的这篇文章主要介绍了ruby-on-rails – 如何从父控制器保存我的子记录?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一堆“小孩”对象已经保存,我想创建一个父对象,通过“亲戚”模式链接到孩子.

这个对象给了我一对多,通过亲戚.

要明确:用户访问“父母”页面,点击创建父母,并提供一个表格,让他们命名父母,并加起来这个父母的四个孩子(通过创建“亲戚”),这些“关系” “也被命名 – 这是重要的一部分.所以,我可以命名关系“步子”或“儿子”.

以下是我到目前为止的代码

  1. class Kid < ActiveRecord::Base
  2. has_many :relatives
  3. has_many :parents,through: :relatives
  4. end
  5.  
  6.  
  7. class Parent < ActiveRecord::Base
  8. has_many :relatives
  9. has_many :kids,through: :relatives
  10.  
  11. accepts_nested_attributes_for :relatives,:reject_if => lambda { |a| a[:content].blank? },:allow_destroy => true
  12. end
  13.  
  14.  
  15. class Relative < ActiveRecord::Base
  16. belongs_to :parent
  17. belongs_to :kid
  18. end
  19.  
  20.  
  21.  
  22. class ParentsController < ApplicationController
  23. before_action :set_parent,only: [:show,:edit,:update,:destroy]
  24. before_action :lookup_kids,only: [:new,:edit]
  25.  
  26. # GET /parents
  27. # GET /parents.json
  28. def index
  29. @parents = Parent.all
  30. end
  31.  
  32. # GET /parents/1
  33. # GET /parents/1.json
  34. def show
  35. end
  36.  
  37. # GET /parents/new
  38. def new
  39. @parent = Parent.new
  40. 4.times { @parent.relatives.build }
  41. end
  42.  
  43. # GET /parents/1/edit
  44. def edit
  45. end
  46.  
  47. # POST /parents
  48. # POST /parents.json
  49. def create
  50. @parent = Parent.new(parent_params)
  51.  
  52. parent_params[:relatives_attributes].each do |k,r|
  53. @parent.relatives.build(r.except(:_destroy))
  54. end
  55.  
  56. respond_to do |format|
  57. if @parent.save
  58. format.html { redirect_to @parent,notice: 'Parent was successfully created.' }
  59. format.json { render :show,status: :created,location: @parent }
  60. else
  61. format.html { render :new }
  62. format.json { render json: @parent.errors,status: :unprocessable_entity }
  63. end
  64. end
  65. end
  66.  
  67. # cut for brevity.
  68.  
  69.  
  70.  
  71. private
  72.  
  73. # Use callbacks to share common setup or constraints between actions.
  74. def set_parent
  75. @parent = Parent.find(params[:id])
  76. end
  77.  
  78.  
  79. def parent_params
  80. params.require(:parent).permit(:name,relatives_attributes: [:parent_id,:kid_id,:relationship,:_destroy])
  81. end
  82. def lookup_kids
  83. @kids = Kid.all #for this nursery.
  84. end
  85. end
  86.  
  87.  
  88.  
  89.  
  90.  
  91. <%= form_for(@parent) do |f| %>
  92. <% if @parent.errors.any? %>
  93. <div id="error_explanation">
  94. <h2><%= pluralize(@parent.errors.count,"error") %> prohibited this parent from being saved:</h2>
  95. <ul>
  96.  
  97. <% @parent.errors.full_messages.each do |message| %>
  98. <li><%= message %></li>
  99. <% end %>
  100. </ul>
  101. </div>
  102. <% end %>
  103.  
  104. <div class="field">
  105. <%= f.label :name %><br>
  106. <%= f.text_field :name %>
  107. </div>
  108.  
  109. <h4>Kids:</h4>
  110. <%= f.fields_for :relatives do |r| %>
  111. <%= r.label :kid %>
  112. <%= r.collection_select :kid_id,@kids,:id,:name,include_blank: true%>
  113. <%= r.label :relationship %>
  114. <%= r.text_field :relationship %>
  115. <%= r.check_Box :_destroy %>
  116. <%= r.label :_destroy,"Remove" %>
  117. <br/>
  118. <% end %>
  119.  
  120. <div class="actions">
  121. <%= f.submit %>
  122. </div>
  123. <% end %>
  124.  
  125.  
  126.  
  127. ActiveRecord::Schema.define(version: 20151030113634) do
  128. create_table "kids",force: :cascade do |t|
  129. t.string "name"
  130. t.datetime "created_at",null: false
  131. t.datetime "updated_at",null: false
  132. end
  133. create_table "parents",null: false
  134. end
  135. create_table "relatives",force: :cascade do |t|
  136. t.string "relationship"
  137. t.integer "parent_id"
  138. t.integer "kid_id"
  139. t.datetime "created_at",null: false
  140. end
  141. add_index "relatives",["kid_id"],name: "index_relatives_on_kid_id"
  142. add_index "relatives",["parent_id"],name: "index_relatives_on_parent_id"
  143. end

当我在父母控制器中“创建”时,我可以看到正确的参数正在通过,但关系记录没有被保存.这不会自动发生吗?

我尝试循环遍历:relatives_attributes,但似乎不适用于“build”.

我怎样才能让“亲戚”记录得到保存?

编辑:添加参数发布:

  1. parent"=>{
  2. "name"=>"Dad","relatives_attributes"=>{
  3. "0"=>{"kid_id"=>"2","relationship"=>"Son","_destroy"=>"0"},"1"=>{"kid_id"=>"","relationship"=>"","2"=>{"kid_id"=>"","3"=>{"kid_id"=>"","_destroy"=>"0"}}}

编辑:我已经更新了,以显示我的最新编辑 – 注意’parent_params [:relatives_attributes] .each do | k,r |’在控制器中.这样现在可以保存孩子的记录,但唯一的问题是它也保存了空白的字段!所以我的“亲戚”记录为空值为孩子记录.我如何阻止它保存空字段(或创建空的相对记录)?

解决方法

答案是建立相对的每个子记录,如下所示:
  1. parent_params[:relatives_attributes].each do |k,r|
  2. @parent.relatives.build(r.except(:_destroy))
  3. end

调用@ parent.save之前.

但是,我仍然有摆脱空白记录的问题.所以如果任何人有这个问题的答案,请在这里评论 – 或者如果有更好或更传统的做法,打我.跟进问题在这里:Why is this reject_if in my model not rejecting blank records?

猜你在找的Ruby相关文章