HTML table 多继承形式,在茧中?
HTML table form with multiple inheritance, in cocoon?
我有一个 table 单元格。
Using cocoon how would I make a form in which each cell belongs_to
the row, column, and table?
例如:
# 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
我现在正在尝试为每场比赛的 :timeslot_id
和 :court_id
使用隐藏的 <input>
,唯一的问题是您无法在保存时间段和场地之前获取 ID。我正在研究的另一个想法是让每个游戏都有一个隐藏的 <input>
它们所在的 row/column。
我终于明白了:
每个单元格都有两个隐藏输入:
<%= cell.hidden_field :row %>
<%= cell.hidden_field :column %>
单元格 belongs_to
关联必须是可选的:
class Cell < ApplicationRecord
belongs_to :table
belongs_to :column, optional: true
belongs_to :row, optional: true
end
并且 Table 有一个 after_save
回调:
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
可能有更好的方法来执行此操作,但我认为这是最简单的方法。
您需要向数据库添加额外的两列:row
和 column
(主要缺点)
我有一个 table 单元格。
Using cocoon how would I make a form in which each cell
belongs_to
the row, column, and table?
例如:
# 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
我现在正在尝试为每场比赛的 :timeslot_id
和 :court_id
使用隐藏的 <input>
,唯一的问题是您无法在保存时间段和场地之前获取 ID。我正在研究的另一个想法是让每个游戏都有一个隐藏的 <input>
它们所在的 row/column。
我终于明白了: 每个单元格都有两个隐藏输入:
<%= cell.hidden_field :row %>
<%= cell.hidden_field :column %>
单元格 belongs_to
关联必须是可选的:
class Cell < ApplicationRecord
belongs_to :table
belongs_to :column, optional: true
belongs_to :row, optional: true
end
并且 Table 有一个 after_save
回调:
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
可能有更好的方法来执行此操作,但我认为这是最简单的方法。
您需要向数据库添加额外的两列:row
和 column
(主要缺点)