Rails正在正确创建一个父对象及其子级,并为每个子对象插入新记录,但随后它将删除子级。父对象本身是另一个已创建对象的子对象。这是模特:
class Pool < ActiveRecord::Base
has_many :addresses, dependent: :destroy
accepts_nested_attributes_for :addresses, allow_destroy: true
end
class Address < ActiveRecord::Base
belongs_to :pool
has_one :ipv4, dependent: :destroy
has_one :ipv6, dependent: :destroy
has_one :mac_address, dependent: :destroy
accepts_nested_attributes_for :ipv4, :ipv6, :mac_address
end
class Ipv4 < ActiveRecord::Base
end
这是控制器
class AddressesController < ApplicationController
def index
end
def create
@pool = Pool.find(params[:pool_id])
@address = @pool.addresses.create(addresses_params)
#Maybe not needed here?
ipv4 = @address.build_ipv4
ipv6 = @address.build_ipv6
mac_address = @address.build_mac_address
redirect_to pool_path(@pool)
end
def destroy
@pool = Pool.find(params[:pool_id])
@address = @pool.addresses.find(params[:id])
@address.destroy
redirect_to pool_path(@pool)
end
private
def addresses_params
params.require(:address).permit(:name, :notes, :hostname, :device_description, :id, :ipv4_attribute
s => [:octet_1, :octet_2, :octet_3, :octet_4, :id], :ipv6_attributes => [:group_1, :group_2, :group_3, :g
roup_4, :group_5, :group_6, :group_7, :group_8, :id], :mac_address_attributes => [:octet_1, :octet_2, :oc
tet_3, :octet_4, :octet_5, :octet_6, :id])
end
end
以下表格:
<%= form_for @address, url: pool_addresses_path(@address.pool) do |f| %>
<div class="field">
<%= f.fields_for :ipv4 do |ipv4_fields| %>
<%= label_tag :ipv4, 'IPv4' %>:
<%= render 'ipv4', :f => ipv4_fields %>
<% end %>
</div>
<div class="field">
<%= f.fields_for :ipv6 do |ipv6_fields| %>
<%= label_tag :ipv6, 'IPv6' %>:
<%= render 'ipv6', :f => ipv6_fields %>
<% end %>
</div>
<div class="field">
<%= f.fields_for :mac_address do |mac_fields| %>
<%= label_tag :mac_address, 'MAC' %>:
<%= render 'mac', :f => mac_fields %>
<% end %>
</div>
<div class="field">
<%= f.label :hostname %>:
<%= f.text_field :hostname %>
</div>
<div class="field">
<%= f.label :device_description %>:
<%= f.text_field :device_description %>
</div>
<div class="field">
<%= f.label :notes %><br>
<%= f.text_area :notes %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
这就是我点击提交按钮时在开发日志中看到的内容:
Started POST "/pools/11/addresses" for 192.168.46.1 at 2014-06-12 13:28:16 -0700
Processing by AddressesController#create as HTML
Parameters: {"utf8"=>"â", "authenticity_token"=>"pnWjd4/Tu8pjZL7PWttvHXmV3SDWPcx4JrSkB50ccws=", "address"=>{"ipv4_attributes"=>{"octet_1"=>"23", "octet_2"=>"", "octet_3"=>"", "octet_4"=>""}, "ipv6_attributes"=>{"group_1"=>"44", "group_2"=>"", "group_3"=>"", "group_4"=>"", "group_5"=>"", "group_6"=>"", "group_7"=>"", "group_8"=>""}, "mac_address_attributes"=>{"octet_1"=>"22", "octet_2"=>"", "octet_3"=>"", "octet_4"=>"", "octet_5"=>"", "octet_6"=>""}, "hostname"=>"cornery", "device_description"=>"peeler", "notes"=>"address notes"}, "commit"=>"Create Address", "pool_id"=>"11"}
Pool Load (0.2ms) SELECT "pools".* FROM "pools" WHERE "pools"."id" = ? LIMIT 1 [["id", 11]]
(0.1ms) begin transaction
SQL (0.5ms) INSERT INTO "addresses" ("created_at", "device_description", "hostname", "notes", "pool_id", "updated_at") VALUES (?, ?, ?, ?, ?, ?) [["created_at", "2014-06-12 20:28:16.554014"], ["device_description", "peeler"], ["hostname", "cornery"], ["notes", "address notes"], ["pool_id", 11], ["updated_at", "2014-06-12 20:28:16.554014"]]
SQL (0.2ms) INSERT INTO "ipv4s" ("address_id", "created_at", "octet_1", "octet_2", "octet_3", "octet_4", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?) [["address_id", 13], ["created_at", "2014-06-12 20:28:16.556819"], ["octet_1", "23"], ["octet_2", ""], ["octet_3", ""], ["octet_4", ""], ["updated_at", "2014-06-12 20:28:16.556819"]]
SQL (0.2ms) INSERT INTO "ipv6s" ("address_id", "created_at", "group_1", "group_2", "group_3", "group_4", "group_5", "group_6", "group_7", "group_8", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) [["address_id", 13], ["created_at", "2014-06-12 20:28:16.559416"], ["group_1", "44"], ["group_2", ""], ["group_3", ""], ["group_4", ""], ["group_5", ""], ["group_6", ""], ["group_7", ""], ["group_8", ""], ["updated_at", "2014-06-12 20:28:16.559416"]]
SQL (0.2ms) INSERT INTO "mac_addresses" ("address_id", "created_at", "octet_1", "octet_2", "octet_3", "octet_4", "octet_5", "octet_6", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?) [["address_id", 13], ["created_at", "2014-06-12 20:28:16.562105"], ["octet_1", "22"], ["octet_2", ""], ["octet_3", ""], ["octet_4", ""], ["octet_5", ""], ["octet_6", ""], ["updated_at", "2014-06-12 20:28:16.562105"]]
(7.0ms) commit transaction
(0.0ms) begin transaction
SQL (0.2ms) DELETE FROM "ipv4s" WHERE "ipv4s"."id" = ? [["id", 15]]
(13.0ms) commit transaction
(0.1ms) begin transaction
SQL (0.2ms) DELETE FROM "ipv6s" WHERE "ipv6s"."id" = ? [["id", 15]]
(9.0ms) commit transaction
(0.1ms) begin transaction
SQL (0.2ms) DELETE FROM "mac_addresses" WHERE "mac_addresses"."id" = ? [["id", 15]]
(8.7ms) commit transaction
Redirected to http://192.168.46.137:3000/pools/11
Completed 302 Found in 82ms (ActiveRecord: 53.1ms)
当Rails重定向到Pool控制器的show方法时,除undefined method octet_1' for nil:NilClass
错误外没有错误,因为没有任何东西,因为Rails删除了所有Address对象的孩子!
有什么线索在这里发生了什么?
谢谢!
答案 0 :(得分:0)
在此处找到线索:nested attributes works for create but fails when update a record [resolved]
我从AddressesController创建操作中删除了构建操作:
class AddressesController < ApplicationController
def index
end
def create
@pool = Pool.find(params[:pool_id])
@address = @pool.addresses.create(addresses_params)
#Maybe not needed here?
#ipv4 = @address.build_ipv4
#ipv6 = @address.build_ipv6
#mac_address = @address.create_mac_address
redirect_to pool_path(@pool)
end
end
我添加了构建操作,因为表单没有显示子对象的字段。他们现在在那里,所以我认为@address = @pool.addresses.create(addresses_params)
正在创造它们?