该表单用于向项目添加项目,但项目不会删除!更糟糕的是,任何更新都会复制所有嵌套表单(1x2 = 2,然后下一次更新会为每个更新4等)
应用/管理/ 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
应用/模型/ 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
应用/模型/ project_feature.rb
class ProjectFeature < ActiveRecord::Base
belongs_to :project
end
应用/模型/ project_feature.rb
class ProjectMention < ActiveRecord::Base
belongs_to :project
end
答案 0 :(得分:6)
在permit_params块中尝试:_destroy
而不是:_destroy => true
。
重复:删除公用文件夹的内容。在开发模式下,您不必预编译资产。
更新:您必须允许相关记录的ID:
project_features_attributes: [:id, :project_id, :description, :_destroy],
project_mentions_attributes: [:id, :project_id, :title, :url, :published, :_destroy]