我正在为竞赛排行榜构建一个rails应用程序。数据模型是
class Tournament
has_many :events
end
class User
has_many :entries
has_many :events, through: :entries
end
class Events
has_many :entries
belongs_to :tournament
end
class Entry
belongs_to :user
belongs_to :event
end
每场锦标赛都有2场比赛,每场比赛最多可以输入4个参赛项目,因此每场锦标赛共有8个参赛项目。我想让用户在一个表单中输入所有8个条目,只需一个提交按钮。
我的路线
resources :entries, except: [:index, :show] do
collection do
match 'create_collection', via: [:create]
end
end
我还在Entries控制器中有一个create_collection方法来处理这些条目,尽管我还没有那么远。我不确定如何使表格正常工作。
_form.html.haml(由views / tournaments / index.html.haml呈现部分,并传递next_events,这是锦标赛的2个事件实例。
.entry-form
= form_tag create_collection_entries_path do |form|
- next_events.each do |event|
= event.name
= fields_for "events[]", event do |f|
- 4.times do
= f.fields_for :entries do |f|
= f.label_tag 'player'
= f.text_field 'player'
= submit_tag "Submit", class: "btn btn-success"
表单按照我的预期显示,但是当我单击提交按钮时,只有最后4个条目在参数中提交且事件ID未被传递
"events"=>[{"entries_attributes"=>
{"0"=>{"player"=>"player5","id"=>"33"},
"1"=>{"player"=>"player6", "id"=>"34"},
"2"=>{"player"=>"player7", "id"=>"35"},
"3"=>{"player"=>"player8", "id"=>"36"}}}],
"commit"=>"Submit"}
如何使用正确的event-id提交所有事件的所有条目参数?