在应用程序中使用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 :(得分:3)
最后在rails 5中本地安装gem应用程序适用于我。 而不是使用github路径
gem 'activeadmin', github: 'activeadmin'
我使用了以下activeadmin版本。
gem 'activeadmin', '~> 1.0.0.pre4'
答案 1 :(得分:0)
解决这个问题的最佳方法是覆盖activeadmin创建操作,你可以做这样的事情
controller do
def create
@course = Course.new(permitted_params[:course])
super
end
end