使用 simple_form 构建具有多个关联输入的双层嵌套表单

Using simple_form to build a double nested form with multiple association inputs

如果之前有人问过这个问题,请指出我的方向。我找不到它。 在此应用程序中,我将向 select 多个团队中的一些用户发送请求,他们应该会收到请求。所以我得到了以下模型:

class Request < ActiveRecord::Base
  has_many :request_teams
  has_many :teams, through: :request_teams
  belongs_to :club
  belongs_to :requester, class_name: User, foreign_key: :requester_id
end

class RequestTeam < ActiveRecord::Base
  belongs_to :request
  belongs_to :team
  has_many :request_coaches
  has_many :coaches, through: :request_coaches
end

class RequestCoach < ActiveRecord::Base
  belongs_to :request_team
  belongs_to :coach, class_name: User
end

我的方法是首先 select 团队并提交它们,然后 select 那些应该收到请求的团队的教练。第一步很好地适用于:

= simple_form_for @request do |f|
  = f.association :teams, as: :check_boxes, collection: @teams

但我无法进行下一步工作。

= simple_form_for @request do |f|
  - for request_team in @request.request_teams
    = f.simple_fields_for :request_teams, request_team do |ft|
      = request_team.team.name
      = ft.input :coaches, as: :select, collection: request_team.team.coaches, input_html: { multiple: 'multiple' }

问题似乎在于它将所有教练聚集在参数中的一个数组中,而不是将它们绑定到适当的 request_team。收到的参数看起来像:"request"=>{"request_teams"=>{"coaches"=>["57","136371"]}}

当我查看呈现的 HTML 时,表单输入名称如下所示:

name="request[request_teams][coaches][]"

我希望它看起来更像:

name="request[request_teams][some_request_team_id_here][coaches][]"

这可能吗?我做错了什么?

这确实是可能的。我只是忘了在 accepts_nested_attributes_for.