ruby-on-rails - 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:insection_params' app/controllers/admin/sections_controller.rb:32:in
update'
我的控制器在参数中有以下内容:
def section_params
params.require(:region).permit(:name, :text_to_order,
location_ids: [], logo_ids: [], employee_ids: [])
end
我在我的参数中尝试了以下内容:
- :policy 我最终得到
ActiveRecord::AssociationTypeMismatch (Media(#70307010602740) 预期,得到“13”,它是 String(#70306928901880) 的一个实例)
- :policy_id 我最终得到
ActionController::UnpermittedParameters(发现未允许的参数::policy)
- policy_ids: [] 我最终得到
ActionController::UnpermittedParameters(发现未允许的参数::policy)
除了创建新属性之外,我如何分配连接到不同模型的人造属性并保存它?
解决方案
改变
= 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_id
到section_params
. 基本上,当您从 select 中选择一个选项时,它将选项值作为选定参数。(在您的情况下,您将值设置为:id)
希望它有效
推荐阅读
- java - Defining Empty String in Avro Schema
- node.js - How to control dmx via usb using nodejs?
- r - How to add columns from another data frame where there are multible matching rows
- azure - Get Cosmosdb Container Collection items using powershell
- python-3.x - cv2.VideoWriter,带有视频文件的上限
- c# - 如何在 Unity C# 脚本中引用另一个项目
- user-interface - 如何在 Flutter 中圆化此面板的边缘?
- swift - 带有 pinToVisibleBounds 的 UICollectionView 组合布局标头奇怪地被单元格遮挡
- python - 并行进程Stata-Python
- ruby-on-rails - 为每个新实例实例化(刷新)的类变量