在Rails

时间:2015-09-20 03:19:18

标签: ruby-on-rails forms nested-forms

我有一个Rails应用程序我正在建立管理幻想网球支架游戏。

以下是我的模特。

玩家将提交选秀权的游戏:

class Game < ActiveRecord::Base
  belongs_to :user
  belongs_to :event
  has_many :picks, dependent: :destroy
  accepts_nested_attributes_for :picks

我匹配两名网球运动员的比赛:

class Match < ActiveRecord::Base
  belongs_to :event
  belongs_to :player_1, class_name: "Player"
  belongs_to :player_2, class_name: "Player"
  has_many :picks, dependent: :destroy

挑选模型。

class Pick < ActiveRecord::Base
  belongs_to :game
  belongs_to :match

我在另一个表单中使用嵌套表单。

<%= form_for(@game) do |f| %>
  <% if @game.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@game.errors.count, "error") %> prohibited this game from being saved:</h2>

      <ul>
      <% @game.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>


  <div class="field">
    <%= f.label :user_id %><br>
    <%= f.collection_select(:user_id, User.all, :id, :name) %>
  </div>
  <div class="field">
    <%= f.label :event_id %><br>
    <%= f.collection_select(:event_id, Event.all, :id, :title) %>
  </div>
  <div class="field">
    <%= f.label :score %><br>
    <%= f.number_field :score %>
  </div>
  <% @matches.each do |g| %>
  <%= f.fields_for :picks do |ff| %>
        <fieldset>
          <%= ff.label :match_id %>
          <%= ff.text_field :match_id %>
          <%= ff.label :winner, "Select the Winner" %> 
          <%= ff.text_field :winner %>
        </fieldset>
        <br>
  <% end %>
  <% end %>   
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

我想使用Match模型中的字段填充Pick模型上的字段。例如,Match有两个玩家,我希望能够将玩家的名字拉到:选择表单中,并且可能会使用f.select。

我在Match模型上有一些玩家数据,我想动态拉入Pick模型。无法弄清楚如何查询数据库并获取Pick嵌套表单要访问的字段属性。任何帮助,将不胜感激。

1 个答案:

答案 0 :(得分:0)

这就是我要做的事。

  1. 摆脱&#34;游戏&#34;
  2. 选择&#34;选择&#34;存储&#34;游戏的模型&#34;信息
  3. 我认为您目前遇到的主要问题是GamePick模型正在尝试做同样的事情。

    #app/models/match.rb
    class Match < ActiveRecord::Base
        # columns id | event_id | name | player_1 | player_2
        belongs_to :event
        has_many :picks
        belongs_to :player_1, class_name: "Player", primary_key: :player_1
        belongs_to :player_2, class_name: "Player", primary_key: :player_2
    
        def players
           [player_1, player_2]
        end
    end
    
    #app/models/pick.rb
    class Pick < ActiveRecord::Base
        # columns id | user_id | match | winner | etc
        belongs_to :user
        belongs_to :match, primary_key: :match
        has_one :winner, primary_key: :winner
    end
    

    这意味着您可以执行以下操作:

    #app/views/picks/new.html.erb
    <% @matches.each do |match| %>
        <%= match.name %>
        <%= form_for match.picks.new do |f| %>
            <%= f.hidden_field :match, match.id %>
            <%= f.collection_select :winner, match.players, :id, :name, prompt: "Who will win?" %>
            <%= f.submit %>
        <% end %>
    <% end %>
    
    #app/controllers/picks_controller.rb
    class PicksController < ActionController::Base
       def new
           @matches = Match.all
       end
       def create
           @pick = Pick.new pick_params
           @pick.save
       end
    
       private
    
       def pick_params
           params.require(:pick).permit(:match, :winner).merge(user_id: current_user)
       end
    end
    

    这将使您能够选择&#34;最简单和最简单的球员可扩展的方式。