首页 > 解决方案 > Rails 属于_to class_name 参数允许

问题描述

我有一个模型:处理所有附件的媒体(带有回形针)。我想要做的是本质上创建一个从不同模型中的媒体中提取的属性:Section。

所以在我的部分模型中,我有以下内容:

Section
 belongs_to :policy, class_name: 'Media',
 optional: true,
 foreign_key: :policy_id

然后在我的表单视图中,我有以下内容:

= f.tb_select :policy, options_from_collection_for_select(text_docs, :id, :attachment_file_name), include blank: 'Select Policy'

下拉菜单有效,并且只显示我希望从我的辅助方法中获得的文件。但是,当我去保存时,我得到:

ActionController::UnpermittedParameters(发现未经许可的参数::policy):
app/controllers/admin/sections_controller.rb:48:in section_params' app/controllers/admin/sections_controller.rb:32:inupdate'

我的控制器在参数中有以下内容:

def section_params
 params.require(:region).permit(:name,  :text_to_order,
  location_ids: [], logo_ids: [], employee_ids: [])
end

我在我的参数中尝试了以下内容:

ActiveRecord::AssociationTypeMismatch (Media(#70307010602740) 预期,得到“13”,它是 String(#70306928901880) 的一个实例)

ActionController::UnpermittedParameters(发现未允许的参数::policy)

ActionController::UnpermittedParameters(发现未允许的参数::policy)

除了创建新属性之外,我如何分配连接到不同模型的人造属性并保存它?

标签: ruby-on-rails

解决方案


改变

= f.tb_select :policy, options_from_collection_for_select(text_docs, :id, :attachment_file_name), include blank: 'Select Policy'

= f.tb_select :policy_id, options_from_collection_for_select(text_docs, :id, :attachment_file_name), include blank: 'Select Policy'

并添加:policy_idsection_params. 基本上,当您从 select 中选择一个选项时,它将选项值作为选定参数。(在您的情况下,您将值设置为:id)

希望它有效


推荐阅读