4

The form works to add items to the Model but items are not deleting! Worse, any update duplicates all of the nested forms (1x2=2, then the next update makes 4 for each, etc.)

app/admin/project.rb

ActiveAdmin.register Project do

  permit_params  :title, :description, :git_url, :demo_url, :version, :lastpublished, :firstpublished,
   project_features_attributes: [:project_id, :description, :_destroy => true],
   project_mentions_attributes: [:project_id, :title, :url, :published, :_destroy => true]

  form do |f|
    f.semantic_errors *f.object.errors.keys
    f.inputs
    f.buttons
  end

  form do |f|
    f.inputs "Project Details" do
      f.inputs :title
      f.inputs :description
      f.inputs :git_url, :default => "http://github.com/"
      f.inputs :demo_url
      f.inputs :version
      f.inputs :firstpublished
      f.inputs :lastpublished      
      f.inputs do
        f.has_many :project_features,
         :allow_destroy => true,
         :heading => 'Features' do |cf|
              cf.input :description
        end
      end
      f.inputs do
        f.has_many :project_mentions,
         :allow_destroy => true,
         :heading => 'Mentions' do |cf|
              cf.input :title 
              cf.input :url
              cf.input :published
        end
      end

    end


    f.actions
  end
end

app/models/project.rb

class Project < ActiveRecord::Base
    has_many :project_features, :dependent => :destroy
    accepts_nested_attributes_for :project_features,
     :reject_if => lambda{ |a| a[:description.blank?] },
     :allow_destroy => true

    has_many :project_mentions, :dependent => :destroy
    accepts_nested_attributes_for :project_mentions,
     :reject_if => lambda{ |a| a[:title.blank?] },
     :allow_destroy => true

    has_many :blogs

end

app/models/project_feature.rb

class ProjectFeature < ActiveRecord::Base
    belongs_to :project

end

app/models/project_feature.rb

class ProjectMention < ActiveRecord::Base
    belongs_to :project

end

1 Answer 1

7

Try :_destroy instead of :_destroy => true in permit_params block.

Duplication: Delete the content of your public folder. In development mode you don't have to precompile the assets.

Update: you have to permit the id of the related record:

project_features_attributes: [:id, :project_id, :description, :_destroy],
project_mentions_attributes: [:id, :project_id, :title, :url, :published, :_destroy]
Sign up to request clarification or add additional context in comments.

3 Comments

changing _destroy didn't work. Also, I don't understand your comment about deleting the public/(404|422|500).html files - what does that to do with ActiveAdmin duplicating the values in my nested forms when performing an update?
Sorry, I thought, you precompiled your assets. In that case a new 'assets' forlder is created in public folder and the has_many form appears twice in development mode.
adding the :id fields to the _attributes did the trick, thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.