【发布时间】:2013-11-21 18:14:46
【问题描述】:
我正在使用 fields_for 表单为 x 数量的关联项目创建选择输入表单。在这种情况下,它是与条目相关联的游戏。我想让用户为条目中的每个游戏选择一个获胜者并存储他们的选择。这是我的表格。
<%= simple_form_for [@contest, @entry] do |f| %>
...一些字段,然后...
<div class="form-group">
<%= f.label :games %><br>
<% Contest.find_by_id(params[:contest_id]) do |contest| %>
<%= f.simple_fields_for :entry_selections do |selection| %>
<table>
<table class="table table-half">
<thead>
<tr>
<th>Away</th>
<th>Home</th>
<th>Choose Winner</th>
</tr>
</thead>
<tbody>
<% contest.matchset.games.each do |game| %>
<tr>
<td><%= game.away_team_name %></td>
<td><%= game.home_team_name %></td>
<td><%= selection.input :team_selected, collection: [[game.away_team_id, game.away_team_name], [game.home_team_id, game.home_team_name]], :label_method => :last, :value_method => :first, :required => false, label: false %></td>
</tr>
<% end %>
<% end %>
</tbody>
</table>
<% end %>
</div>
我正在尝试将每个 :team_selected 保存在名为 entry_selections 的模型上。我认为原因是由于某种原因它无法在那里创建新记录。这是来自服务器日志。
Processing by EntriesController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"HniBF1MQyBdgcNT71vAfyGr4BuJ+OlPBENmVo7FN6hc=", "entry"=>{"name"=>"", "user_id"=>"1", "contest_id"=>"22", "entry_selections"=>{"team_selected"=>"1"}}, "commit"=>"Update Entry", "id"=>"12"}
Entry Load (0.2ms) SELECT "entries".* FROM "entries" WHERE "entries"."id" = ? LIMIT 1 [["id", "12"]]
Unpermitted parameters: entry_selections
我也将它添加到了 entry_controller。
def entry_params
params.require(:entry).permit(:name, :user_id, :contest_id, :matchset_id, entry_selections_attributes: [:game_id, :team_selected, :game_weight])
end
我的 Entries 模型 has_many :entry_selections 和我的 EntrySelections 模型 belongs_to :entry
对于如何存储每场比赛的用户团队选择,有人有什么建议吗?
编辑:添加竞赛模型和 entry_selections 模型
比赛.rb
class Contest < ActiveRecord::Base
has_many :entries
...
validates :league_name, :league_id, :matchset_id, :size, presence: true
end
entry_selection.rb
class EntrySelection < ActiveRecord::Base
belongs_to :entry
validates :team_selected, presence: true
end
【问题讨论】:
-
您能否出示您的参赛模型代码以用于 entry_selections 和竞赛
-
添加了这两个的型号代码
标签: ruby-on-rails nested-attributes model-associations fields-for