2

我正在寻找一种方法来管理表单中的多个嵌入对象。

找到了 bowsersenior 的 formtastic 解决方案

Formtastic 与 Mongoid 嵌入关系

但我无法为 simple_form 做同样的事情

形式:

= semantic_form_for @team do |form|
  = @team.players.each do |player|
    = form.inputs :for => [:players, player] do |player_form|
      = player_form.input :name

此致

样本

class Team
  include Mongoid::Document
  field :name, :type => String
  embeds_many :players
end

class Player
  include Mongoid::Document
  embedded_in :team, :inverse_of => :players
  field :name, :type => String
  field :active, :type=> Boolean # checkboxes
end
4

1 回答 1

3

不确定这是否可行,但您可能想尝试这样的事情:

= simple_form_for @team do |form|
  = f.input :name
  = f.simple_fields_for @team.players do |player_form|
    = player_form.input :name

请记住,在表格出现之前,您必须在团队中创建一个新玩家。在您的控制器(控制器)中:

def new
  @team = Team.new
  8.times { @team.players.new } #for 8 players
end
于 2011-05-19T02:21:46.160 回答