ruby-on-rails - 嵌套表单的解决方案
问题描述
我一直在这个问题上停留了一段时间。
需要使用自定义输入为比赛 类别制作表格。它应该从Information表中获取所有值并构建输入,但棘手的部分是它应该保存到Category_informations表中。
class Competition < ApplicationRecord
has_many :categories
has_many :informations
end
class Category < ApplicationRecord
belongs_to :competetion
has_many :category_informations
has_many :information, through: competition
end
class CategoryInformation
belongs_to :catagory
belongs_to :information
end
class Information < ApplicationRecord
belongs_to :competetion
has_many :category_informations
end
比赛 -> 名称
类别 -> 名称,competition_id
信息 -> 名称,competition_id
类别信息 -> 值、类别 ID、信息 ID
解决方案
听起来你在寻找accepts_nested_attributes_for
https://rubyplus.com/articles/3681-Complex-Forms-in-Rails-5
另外,看看茧宝石。
推荐阅读
- python - SPSS/Python - 访问变量标签
- r - 合并R中的重叠间隔
- django - 创建了一个全新的 Django 项目,尝试运行服务器会生成配置不当的错误
- mysql - 优化复杂的SQL查询
- cordova - Cordova - 如何在应用程序本地化中设置以在 Google Play 商店中显示
- html - CS spritesheet 选择上的边框背景
- java - 如果数据源创建失败,如何测试 Spring Boot *not * 启动
- javascript - res.redirect 在表单提交后显示旧信息?
- java - 启用 @EnableOauth2Sso 后启动容器时出错。在类路径资源中创建名称为“jwtTokenServices”的 bean 时出错
- visual-studio - 如何在 Visual Studio 中的 Eclipse 中放置评论快捷方式(ctrl + /)?