2016-12-05 11 views
0

Я не могу понять, почему моя запись has_many не будет сохранена. Я тестировал с has_one в моделях, и он отлично работает, но когда я меняю его на has_many, он не работает. Я проверил несколько симулятивных записей 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"> 

ответ

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 или только с параметрами