【问题标题】:ActiveAdmin duplicates nested forms and delete checkbox doesn't workActiveAdmin 重复嵌套表单和删除复选框不起作用
【发布时间】:2014-07-26 04:38:17
【问题描述】:

该表单可以将项目添加到模型中,但项目不会删除!更糟糕的是,任何更新都会复制所有的嵌套形式(1x2=2,然后下一次更新为每个 4,等等)

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

【问题讨论】:

    标签: ruby-on-rails-4 activeadmin nested-forms


    【解决方案1】:

    在 permit_params 块中尝试 :_destroy 而不是 :_destroy =&gt; true

    复制:删除公用文件夹的内容。在开发模式下,您不必预编译资产。

    更新:你必须允许相关记录的id:

    project_features_attributes: [:id, :project_id, :description, :_destroy],
    project_mentions_attributes: [:id, :project_id, :title, :url, :published, :_destroy]
    

    【讨论】:

    • 更改 _destroy 无效。另外,我不明白您关于删除 public/(404|422|500).html 文件的评论 - 这与 ActiveAdmin 在执行更新时复制嵌套表单中的值有什么关系?
    • 对不起,我想,你预编译了你的资产。在这种情况下,会在公用文件夹中创建一个新的“资产”文件夹,并且 has_many 表单在开发模式下出现两次。
    • 将 :id 字段添加到 _attributes 成功了,谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-22
    • 2016-10-19
    • 2021-11-27
    • 2020-11-14
    相关资源
    最近更新 更多