首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >HTML表格具有多重继承,在茧中?

HTML表格具有多重继承,在茧中?
EN

Stack Overflow用户
提问于 2017-03-13 07:51:25
回答 1查看 80关注 0票数 0

我有一张有牢房的桌子。

如何使用cocoon创建一个表单,其中每个单元格belongs_to行、列和表?

例如:

代码语言:javascript
运行
复制
# Table
class Roster < ApplicationRecord
    has_many :timeslots, inverse_of: :roster
    has_many :games, inverse_of: :roster

    accepts_nested_attributes_for :timeslots, reject_if: :all_blank, allow_destroy: true
    accepts_nested_attributes_for :games, reject_if: :all_blank, allow_destroy: true
end

# Column
class Court < ApplicationRecord
  belongs_to :roster
  has_many :games

  accepts_nested_attributes_for :games, reject_if: :all_blank, allow_destroy: true
end

# Row
class Timeslot < ApplicationRecord
    belongs_to :roster
    has_many :games

    accepts_nested_attributes_for :games, reject_if: :all_blank, allow_destroy: true
end

# Cell
class Game < ApplicationRecord
    belongs_to :timeslot
    belongs_to :roster
end

我现在尝试一个隐藏的<input>为每个游戏的:timeslot_id:court_id,唯一的问题是,你不能得到的id之前,时间分配和法庭被保存。我要做的另一个想法是让每个游戏都有一个隐藏的行/列的<input>

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-03-15 03:46:21

我终于明白了:每个细胞都有两个隐藏的输入:

代码语言:javascript
运行
复制
<%= cell.hidden_field :row %>
<%= cell.hidden_field :column %>

单元格belongs_to关联必须是可选的:

代码语言:javascript
运行
复制
class Cell < ApplicationRecord
    belongs_to :table
    belongs_to :column, optional: true
    belongs_to :row, optional: true
end

该表有一个after_save回调:

代码语言:javascript
运行
复制
after_save do |table|
    table.rows.each_with_index do |row,row_number|
        table.cells.each do |cell|
            if cell.row-1 == row_number
                cell.row = row
                cell.save()
            end
        end
    end
    table.columns.each_with_index do |column,column_number|
        table.cells.each do |cell|
            if cell.column-1 == column_number
                cell.column = column
                cell.save()
            end
        end
    end
end

也许有更好的方法来做到这一点,但我认为这是最简单的。您需要在数据库中添加两个额外的列:rowcolumn (主要缺点)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42758702

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档