活动管理员嵌套表单创建重复记录

Active admin nested form creates duplicate record

在应用程序中使用 activeadmin,并添加了在创建操作时创建记录两次的嵌套表单,更新操作正常。

使用课程和图像模型。

宝石文件

gem 'rails', '~> 5.0.0'
gem 'activeadmin', github: 'activeadmin'

Course.rb

class Course < ApplicationRecord

  has_many :images, dependent: :destroy

  validates_presence_of :title 

  accepts_nested_attributes_for :images, allow_destroy: true

end

Image.rb

class Image < ApplicationRecord
  belongs_to :course

  mount_uploader :image, ImageUploader

  validates_presence_of :image
end

active_admin/course.rb

ActiveAdmin.register Course do


  permit_params :title, :description, :publish,  images_attributes: [:id, :image, :_destroy]

 index do
  column :title
  column (:description) { |c| raw(c.description) }
  column :publish
  actions
 end

  show do
    attributes_table do
      row :title
      row (:description) { |c| raw(c.description) }
      row :publish

      row "Images" do |c|
        ul do
          c.images.each do |img|
            li do 
              image_tag(img.image.url(:small))
            end
          end
        end
      end


    end
  end

  form do |f|
    f.inputs "Course Details" do
      f.input :title, :placeholder => "eg. General English"
      f.input :description
      f.input :publish
    end
    f.inputs "Images" do
     f.has_many :images, heading: false, allow_destroy: true, new_record: true, html: {multipart: true} do |i|
      i.input :image, :as => :file, :hint => image_tag(i.object.image) 
     end
    end
    f.actions
  end    
end

控制台日志

Processing by Admin::CoursesController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"mK74DdPZ37i3YnMjwq2bej2j/+F0BvAXVJbn5KZamoyGq3A1xeBXPLVCfrPdS4mY9RPYvaC2ZMrZdp36RO3DRw==", "course"=>{"course_type_id"=>"2", "title"=>"General Englisj", "description"=>"this is the description of the course", "publish"=>"1", "images_attributes"=>{"0"=>{"image"=>#<ActionDispatch::Http::UploadedFile:0xd585c98 @tempfile=#<Tempfile:/tmp/RackMultipart20160728-8817-5eo5a6.jpg>, @original_filename="pVxrlkgM3GDl.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"course[images_attributes][0][image]\"; filename=\"pVxrlkgM3GDl.jpg\"\r\nContent-Type: image/jpeg\r\n">}}}, "commit"=>"Create Course"}
  AdminUser Load (1.3ms)  SELECT  "admin_users".* FROM "admin_users" WHERE "admin_users"."id" =  ORDER BY "admin_users"."id" ASC LIMIT   [["id", 1], ["LIMIT", 1]]
  Location Load (0.9ms)  SELECT "locations".* FROM "locations" WHERE 1=0
  CACHE (0.0ms)  SELECT "locations".* FROM "locations" WHERE 1=0
   (0.4ms)  BEGIN
  SQL (14.3ms)  INSERT INTO "courses" ("title", "description", "created_at", "updated_at", "course_type_id") VALUES (, , , , ) RETURNING "id"  [["title", "General Englisj"], ["description", "this is the description of the course"], ["created_at", 2016-07-28 08:29:24 UTC], ["updated_at", 2016-07-28 08:29:24 UTC], ["course_type_id", 2]]
  SQL (58.6ms)  INSERT INTO "images" ("image", "course_id", "created_at", "updated_at") VALUES (, , , ) RETURNING "id"  [["image", "pVxrlkgM3GDl.jpg"], ["course_id", 36], ["created_at", 2016-07-28 08:29:24 UTC], ["updated_at", 2016-07-28 08:29:24 UTC]]
  SQL (0.7ms)  INSERT INTO "images" ("image", "course_id", "created_at", "updated_at") VALUES (, , , ) RETURNING "id"  [["image", "pVxrlkgM3GDl.jpg"], ["course_id", 36], ["created_at", 2016-07-28 08:29:24 UTC], ["updated_at", 2016-07-28 08:29:24 UTC]]
   (21.4ms)  COMMIT
Redirected to http://localhost:3000/admin/courses/36
Completed 302 Found in 3578ms (ActiveRecord: 174.0ms)

因此同一图像在应用程序中生成两次

克服这个问题的最佳方法是覆盖 activeadmin 创建操作,您可以这样做

controller do
  def create
    @course = Course.new(permitted_params[:course])
    super
  end
end

最终在 rails 5 应用程序中本地安装 gem 对我有用。 而不是使用 github 路径

gem 'activeadmin', github: 'activeadmin'

我用过以下activeadmin版本。

gem 'activeadmin', '~> 1.0.0.pre4'