【发布时间】:2016-07-28 05:55:05
【问题描述】:
在应用程序中使用 activeadmin,并添加嵌套表单,在创建操作时创建两次记录,更新操作正常。
使用课程和图像模型。
宝石文件
gem 'rails', '~> 5.0.0'
gem 'activeadmin', github: 'activeadmin'
课程.rb
class Course < ApplicationRecord
has_many :images, dependent: :destroy
validates_presence_of :title
accepts_nested_attributes_for :images, allow_destroy: true
end
图片.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)
所以相同的图像会在应用程序中生成两次
【问题讨论】:
-
您确定创建了两条记录吗?也许一张图片在表单中显示了两次
-
是的,检查了数据库,插入查询被调用了两次对于单个嵌套记录,因此确保创建了两条记录
-
在控制台中检查发送了哪些参数并将它们添加到问题中。并检查提交表单时是否只发送了一个请求
-
是的,请求只发送了一次,但“插入”被调用了两次,将日志添加到问题中
-
我在 Rails 4.2.6 上,这也发生在我身上。把我的头发拉出来……
标签: ruby-on-rails activeadmin ruby-on-rails-5