2016-07-28 68 views
1

在应用程序中使用activeadmin,并添加了嵌套表单,该表单在创建操作时创建了两次记录,更新操作正在启动。活动管理嵌套表单创建重复记录

使用课程和图像模型。

的Gemfile

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" = $1 ORDER BY "admin_users"."id" ASC LIMIT $2 [["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 ($1, $2, $3, $4, $5) 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 ($1, $2, $3, $4) 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 ($1, $2, $3, $4) 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) 

使同一图像获取产生两次申请

+0

您确定有两条记录被创建吗?也许一个图像以 – chumakoff

+0

的形式显示两次,是用数据库检查,插入到单个嵌套记录调用两次的查询中,所以确保两个记录创建了 – chaitanya

+0

在控制台中检查发送了什么参数并将它们添加到问题中。并检查您提交表单时是否只有一个请求发送 – chumakoff

回答

3

最后在rails 5应用程序中为我安装gem。 代替使用的github路径

gem 'activeadmin', github: 'activeadmin' 

我已经使用以下activeadmin版本。

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

这个工程。看起来这是一个已知的问题,正如他们的GitHub所报道的那样:https://github.com/activeadmin/activeadmin/issues/4548 – Ninjarabbi

0

克服这是给你覆盖activeadmin创建行动的最佳途径,你可以做这样的事情

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