ActiveAdmin不保存has_many嵌套对象 [英] ActiveAdmin not saving has_many nested object

查看:75
本文介绍了ActiveAdmin不保存has_many嵌套对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎无法找出为什么我的has_many记录无法保存的原因.我已经在模型中使用has_one进行了测试,它可以正常工作,但是当我将其更改为has_many时,它将无法正常工作.我检查了一些类似的stackoverflow帖子,并且我似乎有正确的用法,但是它不起作用. activeadmin中的嵌套表格未保存更新& ActiveAdmin表单未保存嵌套对象

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

这是用于Rails version 5.1.0.alphaActiveadmin 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)

这是我的信息

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浏览器工作

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"> 

推荐答案

我能够通过添加 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或仅适用于参数

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

这篇关于ActiveAdmin不保存has_many嵌套对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆