2016-07-28 7 views
1

Mit ActiveAdmin in der Anwendung, und verschachtelte Formular hinzugefügt, die Datensatz erstellt zweimal beim Erstellen Aktion, Update Aktion ist gut.Verschachteltes Active-Admin-Formular erstellt doppelten Datensatz

Verwenden von Kurs- und Bildmodell.

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 

Konsolprotokoll

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) 

so gleiches Bild zweimal in Anwendung bekommen erzeugen

+0

Sind Sie sicher, dass zwei Datensätze erstellt wurden? Vielleicht wird ein Bild zweimal in der Form angezeigt – chumakoff

+0

ja mit Datenbank überprüft, in Abfrage einfügen bekam zweimal für einzelne verschachtelte Datensatz aufgerufen, so dass zwei Datensätze erstellt – chaitanya

+0

in der Konsole überprüfen, welche Parameter gesendet werden, und fügen Sie sie der Frage hinzu. Und überprüfen Sie, ob nur eine Anfrage gesendet wird, wenn Sie das Formular senden – chumakoff

Antwort

3

Endlich installieren Juwel lokal in Schienen 5 Anwendung funktioniert für mich. statt Github Pfad der Verwendung

gem 'activeadmin', github: 'activeadmin' 

i folgende activeadmin Version verwendet haben.

+2

Das funktioniert. Es scheint, dass es ein bekanntes Problem ist, wie auf ihrem GitHub berichtet: https://github.com/activeadmin/activeadmin/issues/4548 – Ninjarabbi

0

Der beste Weg, dies zu überwinden, ist für Sie die activeadmin erstellen Aktion außer Kraft zu setzen, können Sie so etwas wie diese

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