我似乎無法找出爲什麼我的has_many
記錄不會保存。我已在模型中使用has_one
進行測試,並且它工作正常,但是當我將其更改爲has_many
時,它不起作用。我檢查了幾個simulate stackoverflow的帖子,我似乎有這個正確的,但它不工作。 Nested form in activeadmin not saving updates & ActiveAdmin Form not saving nested objectActiveAdmin不保存has_many嵌套對象
這是Rails version 5.1.0.alpha
和Activeadmin version 1.0.0.pre4
我沒有得到任何錯誤,我也不看嵌套對象獲得創建,雖然我看到PARAMS,當我在這裏創建一個對象是輸出
Started POST "/admin/abq_districts" for ::1 at 2016-12-05 10:04:44 -0700
Processing by Admin::AbqDistrictsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"bHzfUG8xIMI66tTHNwSmL5FVnaa4nyuzJ3AM8tnLJSq69TLP1o8iUcLLeNnoS0FVgA8ju3x7Ioc+EV4xRv/T7Q==", "abq_districts_district"=>{"name"=>"test district", "title"=>"", "sub_title"=>"", "paragraph"=>"", "sections_attributes"=>{"0"=>{"text"=>"test section"}}}, "commit"=>"Create District"}
AdminUser Load (1.0ms) SELECT "admin_users".* FROM "admin_users" WHERE "admin_users"."id" = $1 ORDER BY "admin_users"."id" ASC LIMIT $2 [["id", 1], ["LIMIT", 1]]
(0.2ms) BEGIN
SQL (0.4ms) INSERT INTO "abq_districts_districts" ("name", "title", "sub_title", "paragraph", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id" [["name", "test district"], ["title", ""], ["sub_title", ""], ["paragraph", ""], ["created_at", "2016-12-05 17:04:44.756215"], ["updated_at", "2016-12-05 17:04:44.756215"]]
(0.6ms) COMMIT
Redirected to http://localhost:3000/admin/abq_districts/41
Completed 302 Found in 34ms (ActiveRecord: 2.1ms)
這裏是我的信息
ActiveAdmin.register
ActiveAdmin.register AbqDistricts::District, as: 'ABQ Districts' do
permit_params :name, :title, :sub_title, :paragraph, sections_attributes: [ :text, :_destroy ]
index do
selectable_column
id_column
column :name
column :title
column :sub_title
column :paragraph
column :created_at
column :updated_at
actions
end
filter :name
filter :title
filter :updated_at
filter :created_at
form do |f|
f.inputs "Admin Details" do
f.input :name
f.input :title
f.input :sub_title
f.input :paragraph
f.inputs do
f.has_many :sections, heading: 'Sections', allow_destroy: true, new_record: true do |a|
a.input :text
end
end
end
f.actions
end
end
abq_districts.rb
module AbqDistricts
def self.table_name_prefix
'abq_districts_'
end
end
district.rb
class AbqDistricts::District < ApplicationRecord
has_many :sections, foreign_key: :abq_districts_district_id
accepts_nested_attributes_for :sections, allow_destroy: true
end
section.rb
class AbqDistricts::Section < ApplicationRecord
belongs_to :abq_districts_district
validates_presence_of :abq_districts_district
end
UPDATE
我只是在軌測試控制檯和它工作得很好(仍然沒有通過ActiveAdmin瀏覽器中工作
2.3.0 :001 > params = {"name"=>"test district", "title"=>"", "sub_title"=>"", "paragraph"=>"", "sections_attributes"=>{"0"=>{"text"=>"test section"}}}
=> {"name"=>"test district", "title"=>"", "sub_title"=>"", "paragraph"=>"", "sections_attributes"=>{"0"=>{"text"=>"test section"}}}
2.3.0 :002 > d = AbqDistricts::District.create(params)
(0.2ms) BEGIN
SQL (0.5ms) INSERT INTO "abq_districts_districts" ("name", "title", "sub_title", "paragraph", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id" [["name", "test district"], ["title", ""], ["sub_title", ""], ["paragraph", ""], ["created_at", "2016-12-05 18:58:02.973482"], ["updated_at", "2016-12-05 18:58:02.973482"]]
SQL (2.2ms) INSERT INTO "abq_districts_sections" ("text", "abq_districts_district_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id" [["text", "test section"], ["abq_districts_district_id", 63], ["created_at", "2016-12-05 18:58:02.979025"], ["updated_at", "2016-12-05 18:58:02.979025"]]
(0.4ms) COMMIT
=> #<AbqDistricts::District id: 63, name: "test district", title: "", sub_title: "", paragraph: "", created_at: "2016-12-05 18:58:02", updated_at: "2016-12-05 18:58:02">