Rails 下拉 has_many 通过

Rails Dropdown has_many through

我无法在连接 table (document_configuration) 中保存 ID。

我有树模型:

document.rb

belongs_to :languages
has_many :document_configurations
has_many :document_catalogs, through: :document_configurations

accepts_nested_attributes_for :document_catalogs
accepts_nested_attributes_for :document_configurations

document_catalog.rb

has_many :document_configurations
has_many :documents, through: :document_configurations

document_configuration.rb

belongs_to :document
belongs_to :document_catalog

所以,我想在我的 document_form 中获得所有 document_catalog 的列表,所以,当我创建一个新文档时,我可以包含相应的目录。

这是我的表格:

<div class="form-group">
  <%= f.select :document_catalog_ids, DocumentCatalog.all.collect {|x| [x.name, x.id]}, {}%>
</div>

正在列出我想要的目录。

这是我的控制器:

def new
 @document = Document.new
 @document.document_catalogs.build
end

def document_params
  params.require(:document).permit(:name, :description, :document_file,
  :language_id, {:document_catalog_ids=>[]}) #I tried this too: :document_catalog_ids=>[] without the {}
end

我刚收到这个错误:Unpermitted parameter: document_catalog_ids 我真的需要在 [=22] 中保存 document_iddocument_catalog_id =] 模型.

另一件事是:我需要在创建、更新和销毁方法中添加任何其他内容吗?

处理未经许可的密钥

默认情况下,未明确允许的参数密钥将记录在开发和测试环境中。在其他环境中,这些参数将被简单地过滤掉并忽略。

此外,可以通过更改环境文件中的 config.action_controller.action_on_unpermitted_parameters 属性 来更改此行为。如果设置为 :log 将记录不允许的属性,如果设置为 :raise 将引发异常。

在 GitHub 的文档中找到了这个:https://github.com/rails/strong_parameters#handling-of-unpermitted-keys