当未选择任何选项时,collection_select 不会让 ActiveRecord 更新其集合
我在两个模型之间有 has_and_belongs_to_many 关联。
我使用 collection_select 以表单形式表示此关联中的另一个模型。当我尝试取消选择 collection_select 中的选项时,collection_select 不会发布其空值,因此,它不会让 ActiveRecord 将任何更新子句传递到数据库。
例如参数就像; 当选择选项时; 参数: {"name_of_model"=>{"name"=>"de2", "other_model_ids"=>["1", "3"]}
当没有选择时; 参数: {"name_of_model"=>{"name"=>"de2" }
有谁知道解决方法吗?
I have has_and_belongs_to_many association between two models.
I made use of collection_select to represent the other model in this association in a form. When I tried to deselect options in collection_select, collection_select doesn't post its empty value and, thus, it doesn't let ActiveRecord pass any update clause to the database.
e.g. Parameters are like;
When onptions selected;
Parameters: {"name_of_model"=>{"name"=>"de2", "other_model_ids"=>["1", "3"]}
When none selected;
Parameters: {"name_of_model"=>{"name"=>"de2" }
Does anyone know a workaround for this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果没有将
other_model_ids
参数传递给控制器操作,则需要将其设置为空数组:这将确保如果没有选择,则模型将被正确更新,以便它没有关联的模型 ID。
You need to set the
other_model_ids
parameter to be an empty array if it is not passed to the controller action:This will ensure that if there is no selection then the model will be updated correctly so that it has no associated model IDs.