0

我有以下型號我使用...如何設置一個用於在Rails中添加關聯記錄的靜態表單?

遊戲

class Game < ActiveRecord::Base 

    has_many :workout 
    has_many :participations 

end 

參與

class Participation < ActiveRecord::Base 

    belongs_to :player 
    belongs_to :game 

end 

鍛鍊

class Workout < ActiveRecord::Base 

    belongs_to :player 
    belongs_to :game 
    has_many :measurables 

end 

class Measurable < ActiveRecord::Base 

    belongs_to :workout 

end 

Workout是目前Player嵌套資源。通常,我會在Player之間創建一個Workout記錄。我還會使用寶石nested_fields_forMeasurable添加到Workout,但這樣可以讓我一次只能爲一個玩家添加/移除一次可測量的值。我正在嘗試爲用戶編寫一個表單,以便爲參與遊戲的每個玩家編輯多個測量值。

例表

Player   | Height | Weight | Hand Span | 
Player 1's Name  [input box] [input box]  [input box] 
Player 2's Name  [input box] [input box]  [input box] 
Player 3's Name  [input box] [input box]  [input box] 
Player 4's Name  [input box] [input box]  [input box] 

我到目前爲止建立了我的意見/遊戲edit_measurables.rb目錄和games_controller稱爲#edit_measurables的作用下視圖。我打算在這裏創建或查找對應於GameWorkout並構建上面顯示的表單。雖然我不確定該從哪裏出發。我認爲我的模特和協會有不正確的組織。

更新:

我已經成功地相處遠一點,但我仍然運行到的幾個問題,雖然一些這個問題的範圍之外。

我想構建我想通過以下方式來處理的形式...

<table> 
    <thead> 
    <tr> 
     <th>Player</th> 
     <th>Height</th> 
     <th>Weight</th> 
     <th>Hand</th> 
    </tr> 
    </thead> 
    <tbody> 
    <% @game.participations.each do |participant| %> 
     <% player = participant.player %> 

     <% simple_form_for participant.player do |player_form| %> 
     <% workout = Workout.find_or_create_by(...) %> 
     <% @measurables.each {|m| workout.measurables.build(...) unless workout.measurables.any? { ... } } %> 

     <tr> 
     <td><%= player.full_name %></td> 

     <% player_form.nested_fields_for :measurables, workout.measurables, wrapper_tag: :td do |measurable_form| %> 
      <td><%= measurable_form.input :value, label: false %></td> 
     <% end %> 
     </tr> 
    <% end %> 
    </tbody> 
</table> 
+0

有趣。你有沒有任何代碼?我有代碼可以給你你需要的東西,但是它不能添加動態值等。 –

+0

'<%game.participations.each do | participation | %> <%鍛鍊= participation.game.workout%> <%=的form_for participation.player做| player_form | %> <%= fields_for player.measureables.where(鍛鍊:鍛鍊)do | measurable_form | %> <%= measurable_form.text_field:高度%> <% end %> <% end %> <% end %>'這將意味着我需要提交按鈕的每一行,雖然我認爲 - 這是不是我想要的.. – daveomcd

+0

謝謝你,讓我寫一個答案。它可能不會是你所需要的,但可能會給出想法 –

回答

1
<% game.participations.each do |participation| %> 
    <% workout = participation.game.workout %> 
    <%= form_for participation.player do |player_form| %> 
    <%= fields_for player.measureables.where(workout: workout) do |measurable_form| %> 
     <%= measurable_form.text_field :height %> 
    <% end %> 
    <% end %> 
<% end %> 

所以,你在做什麼,是給player爲一組的measurables每個workout,其中一個game有一個?

問題這裏要說的是,你還沒有相關的measurablesgame & player,特別是通過participations。我認爲這就是你看到問題的原因;您的measurables必須從另一個無關的表格中提取。

如果是這種情況,爲什麼不把測量值放在participation模型中?

「has_one」鍛鍊意味着只有一個鍛鍊的遊戲...這意味着遊戲可以運行belong_to?如果是這樣的話,有人蔘與的遊戲將能夠存儲他們的衡量標準爲遊戲在他們的Participation模型...

-

#app/views/games/edit.html.erb 
<%= form_for @game do |f| %> 
    <%= f.fields_for :participations do |p| %> 
     <%= p.object.player.name unless p.object.new_record? %> 
     <%= p.collection_select :player_id, Player.all, :id, :name if p.object.new_record? %> 
     <%= p.number_field :measurable_height %> 
     <%= p.number_field :measurable_weight %> 
     <%= p.number_field :measurable_hand_span %> 
    <% end %> 
    <%= f.submit %> 
<% end %> 

這將讓你measurables關聯直接:

#app/models/game.rb 
class Game < ActiveRecord::Base 
    belongs_to :workout 
    has_many :participations 
    has_many :players, through: :participations 
end 

#app/models/participation.rb 
class Participation < ActiveRecord::Base 
    #id | player_id | game_id | measurable_height | measurable_weight | measurable_hand_span | created_at | updated_at 
    belongs_to :player 
    belongs_to :game 
end 

用下面的控制器:

#app/controllers/games_controller.rb 
class GamesController < ApplicationController 

    def new 
    @game = Game.new 
    @game.participations.build #-> this would be needed to "add" extra fields 
    end 

    def edit 
    @game = Game.find params[:id] 
    end 

    def update 
    @game = Game.find params[:id] 
    @game.update update_params 
    end 

    private 

    def update_params 
    params.require(:game).permit(partipations_attributes: [:measurable_height, :measurable_weight, :measurable_hand_span]) 
    end 
end 
+0

哇,感謝您抽出時間 - 讓我看看它。我會注意到,直到我開始嘗試找出這個問題時,我才知道鍛鍊協會*應該是has_many,而不是has_one。 – daveomcd

+0

好吧,讓我的答案陷入危險。你能告訴我更多關於「遊戲」/「鍛鍊」和他們如何一起工作的信息嗎? –

+0

「鍛鍊」屬於「遊戲」和「玩家」。每個「遊戲」都有一組「參與者」。我試圖描述的是一場足球比賽。在這場比賽中,不同的球員將參加比賽,但也會有一個「鍛鍊」,每個球員都有很多不同的測量項目(例如:身高,體重,手跨度,手臂長度等)。有些人會選擇不測量儘管如此。我試圖用這個表格創建一個輸入框,用於爲每個玩家選擇具體的可測量的可能性。因此,用戶可以通過輸入所有值來製表符。 – daveomcd

相關問題