问题
I cannot seem to find out why my has_many
record will not save. I've test with has_one
in the models and it works fine but when I change it to has_many
it doesn't work. I've check a few simular stackoverflow posts and I seem to have this correct but it doesn't work. Nested form in activeadmin not saving updates & ActiveAdmin Form not saving nested object
This is for Rails version 5.1.0.alpha
and Activeadmin version 1.0.0.pre4
I do not get any errors nor do I see the nested object get created though I see the params when I create an object here is the output
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)
here is my info
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
I just testing in the rails console and it worked fine (still does not work through ActiveAdmin browser
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">
回答1:
I was able to patch by adding in ActiveAdmin.register
controller do
def create
@section = AbqDistricts::District.create!(permitted_params[:abq_district].as_json)
redirect_to admin_faq_sections_path, notice: "Section was successfully created!"
end
end
This is just a patch. Notice and the only way it worked is if I used .as_json
it would not work with to_json
or just the params alone
来源:https://stackoverflow.com/questions/40979595/activeadmin-not-saving-has-many-nested-object