ActiveAdmin unpermitted param on 有很多:通过关联

ActiveAdmin unpermitted param on has many: through association

我回答了大约 12 个与此类似的 Whosebug 问题,但其中 none 似乎对我有所帮助。

活动管理员: Article.rb

permit_params tag_ids: []
...
f.input :tag, as: :check_boxes, collection: Tag.all.map { |h|  [h.name, h.id] }

标签:

class Tag < ApplicationRecord
  has_many :taggings
  has_many :articles, through: :taggings

标签:

class Tagging < ApplicationRecord
  belongs_to :article
  belongs_to :tag
end

文章:

class Article < ApplicationRecord
  belongs_to :category
  has_many :taggings
  has_many :tags, through: :taggings

我尝试允许以下内容:

获得:

found unpermitted parameter: hashtag

少了点东西!

更新 1

这是我的日志:

[INFO ] === Processing by Admin::ArticlesController#update as HTML
[INFO ]   Parameters: {"utf8"=>"✓", "authenticity_token"=>"...", "article"=>{"tag"=>["", "1", "2"] ...}, "commit"=>"Update Article", "id"=>"1"}
[DEBUG]   AdminUser Load (2.1ms)  SELECT  "admin_users".* FROM "admin_users" WHERE "admin_users"."id" =  ORDER BY "admin_users"."id" ASC LIMIT   [["id", 1], ["LIMIT", 1]]
[DEBUG]   Article Load (0.4ms)  SELECT  "articles".* FROM "articles" WHERE "articles"."id" =  LIMIT   [["id", 1], ["LIMIT", 1]]
[DEBUG] Unpermitted parameter: tag

首先,accepts_nested_attributes_for 通过添加这个,"article"=>{"tag"=>["", "1", "2"] ...} 将转换为 "article"=>{"tags_attributes"=>["", "1", "2"] ...}

class Article < ApplicationRecord
   belongs_to :category
   has_many :taggings
   has_many :tags, through: :taggings

   accepts_nested_attributes_for :tags, :allow_destroy => true
end

在您的文章控制器中,

在强参数列表中添加tags_attributes: [:id]。这将解决您的问题。

编码愉快。

我设法解决了。有两种选择可以达到预期的结果。

Add/remove 动态来自 AA 的标签。

感谢@user100693 的提示,通过将 accepts_nested_attributes 添加到我的文章模型中,我能够动态地 add/remove 所需的标签,但是我必须添加

accepts_nested_attributes_for :taggings, allow_destroy: true 不是 :tags

允许的属性是:

taggings_attributes: [:id, :_destroy, :tag_id]

以及AA表格代码:

  f.has_many :taggings, allow_destroy: true, new_record: true do |h|
    h.input :tag
  end

多个 select 复选框

这是我想要达到的结果 - 通过复选框将文章分配给标签。原来我需要对我的原始代码做的唯一改变(见问题)是这样的:

活跃管理员Article.rb

允许的参数:tag_ids:[]

形式:f.input :tags, as: :check_boxes

而不是遍历 Hash。就是这样。