ActiveAdmin不保存has_many嵌套对象

时间:2016-12-05 17:08:43

标签: ruby-on-rails activeadmin nested-attributes

我似乎无法找出为什么我的has_many记录无法保存。我已在模型中使用has_one进行测试,但效果很好,但当我将其更改为has_many时,它无法正常工作。我已经检查了几个类似的stackoverflow帖子,我似乎有这个正确但它不起作用。 Nested form in activeadmin not saving updates& ActiveAdmin Form not saving nested object

适用于Rails version 5.1.0.alphaActiveadmin 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

更新

我只是在rails控制台中测试它运行正常(仍然无法通过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"> 

1 个答案:

答案 0 :(得分:0)

我可以通过添加 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

这只是一个补丁。注意并且唯一的方法是使用.as_json 它不适用于to_json或仅适用于params