Rails - 深度嵌套_属性 - 未记录在数据库

时间:2016-01-12 18:35:27

标签: ruby-on-rails nested

我遇到了一些有关在数据库中记录3个nested_attributes_for的问题。

在我的应用程序中,我有3个型号:Deal,Pool,Facility

class Deal < ActiveRecord::Base
has_many :pools, :dependent => :destroy
accepts_nested_attributes_for :pools, :reject_if => lambda { |a| a[:name].blank? }, :allow_destroy => true
end

class Pool < ActiveRecord::Base
belongs_to :deal
has_many :facilities, :dependent => :destroy
accepts_nested_attributes_for :facilities, :reject_if => lambda { |a| a[:name].blank? }, :allow_destroy => true
end

class Facility < ActiveRecord::Base
belongs_to :pool
has_many :facilityschedules, :dependent => :destroy
accepts_nested_attributes_for :facilityschedules, :reject_if => lambda { |a| a[:name].blank? }, :allow_destroy => true
end

这是我的Deal控制器新动作:

  def new
@deal = Deal.new
1.times do
  pool = @deal.pools.build
    1.times { pool.facilities.build }
end
end

这是我在交易控制器中的deal_params:

def deal_params
  params.require(:deal).permit(:name, pools_attributes: [:id, :name, :number, :deal_id, :_destroy, facilities_attributes: [:id, :name, :pool_id, :_destroy, ]])
end

编辑: 以下是rails console中的消息:

Started POST "/deals" for ::1 at 2016-01-12 21:19:26 +0100
Processing by DealsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"s3CQBTaa3lo/dzYDQKdKjs4XcE3HLSDYm/B6QjVi9JKns4x4LRjYKRQ4Fc+r4clyWQcrgmGrgrHsSGFtUEkwjg==", "deal"=>{"name"=>"Deal 26", "pools_attributes"=>{"0"=>{"name"=>"Pool 26", "_destroy"=>"0"}}, "facilities"=>{"name"=>"Facility 26", "_destroy"=>"0"}}, "commit"=>"Create Deal"}
Unpermitted parameter: facilities
   (0.1ms)  begin transaction
  SQL (0.4ms)  INSERT INTO "deals" ("name", "created_at", "updated_at") VALUES (?, ?, ?)  [["name", "Deal 26"], ["created_at", "2016-01-12 20:19:26.986981"], ["updated_at", "2016-01-12 20:19:26.986981"]]
  SQL (0.2ms)  INSERT INTO "pools" ("name", "deal_id", "created_at", "updated_at") VALUES (?, ?, ?, ?)  [["name", "Pool 26"], ["deal_id", 21], ["created_at", "2016-01-12 20:19:26.988843"], ["updated_at", "2016-01-12 20:19:26.988843"]]
   (1.7ms)  commit transaction
Redirected to http://localhost:3000/deals/21
Completed 302 Found in 10ms (ActiveRecord: 2.4ms)

设施似乎是非负责任的参与者。我怎么能改变它? 这是我的观点形式:

    <%= form_for(@deal) do |f| %>
  <% if @deal.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@deal.errors.count, "error") %> prohibited this deal from being saved:</h2>

      <ul>
      <% @deal.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>



  <div class="field">
    <%= f.label :name, "Deal name"%>
    <%= f.text_field :name %>
    <br/>
  </div>


<div>
  <%= f.fields_for :pools do |builder|%>
  <%= builder.label :name, "Pool name" %>
  <%= builder.text_field :name, :rows => 3 %>
  <%= builder.check_box :_destroy %>
  <%= builder.label :_destroy, "Remove Pool" %>
  <br/>
  <% end %>
 </div>

 <div>
  <%= f.fields_for :facilities do |builder|%>
  <%= builder.label :name, "Facility name" %>
  <%= builder.text_field :name, :rows => 3 %>
  <%= builder.check_box :_destroy %>
  <%= builder.label :_destroy, "Remove Facility" %>
  <br/>
  <% end %>
 </div>


  <div class="actions">
    <%= f.submit %>
  </div>


<% end %>

当我尝试使用池和工具(以单一形式)创建交易时,交易和池都在数据库中创建。然而,该设施尚未创建。我做错了什么?

非常感谢! :)

注意:不要考虑&#34; facilityschedules&#34;。一旦我解决了这个问题,它就会成为第四个属性:)

1 个答案:

答案 0 :(得分:0)

看起来你不是在池下嵌套设施。应该是一个简单的修复,只是池下的巢设施。这样的事情需要改变。

<%= builder.fields_for :facilities do |fbuilder|%>

它需要在构建器块的范围内。

相关问题