mongoid,embedy_many,simple_form

时间:2011-04-16 09:27:00

标签: mongoid simple-form

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

找到了一个由bowsersenior开发的一种解决方案

Formtastic with Mongoid embedded_in relations

但我无法对simple_form

做同样的事情

formtastic:

= 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

1 个答案:

答案 0 :(得分: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