0

私はセルを備えたテーブルを持っています。私は各セルフォームbelongs_to行、列、およびテーブル作成する方法をを使用複数の継承を持つHTML表形式、繭で?

?例については

# 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のための隠された<input>で今しようと:court_idています唯一の問題は、タイムスロットと、裁判所が保存される前に、あなたがIDを取得することはできませんです。各ゲームは、彼らがしている行/列の隠された<input>を持っているため、私が取り組んでミリアンペア、他のアイデアはにある

+0

ブートストラップのようなグリッドシステムを使用する方が簡単でしょうか?しかし、とにかく:実際のhtmlの 'table'(それは単なる難しい)を使うことができないべきではない理由はありません。だからあなたはどこにいらっしゃいましたか? – nathanvda

+0

私はそれを理解することができたと思う。バックエンドのルビーよりもフロントエンドで多くのjavascriptをやっていました。私の問題は、各セルを列と行に属するようにすることでした。 – DrevanTonder

答えて

0

私はついにそれを考え出した:。

<%= cell.hidden_field :row %> 
<%= cell.hidden_field :column %> 
: 各セルには、2つの隠し入力を持っていました

細胞belongs_toアソシエーションは任意でなければならなかった:

class Cell < ApplicationRecord 
    belongs_to :table 
    belongs_to :column, optional: true 
    belongs_to :row, optional: true 
end 

テーブルが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 

おそらくもっと良い方法がありますが、これは最も簡単だと思います。 データベースに余分な2列を追加する必要があります:rowcolumn(主な欠点)