2010-12-22 65 views
1

是否有任何快速的方法来为embeds_many-embedded_in关系的窗体? 我有以下几点:Formtastic与Mongoid嵌入关系

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 
end 

我想创建一个团队,为玩家嵌入的编辑形式。看到https://github.com/bowsersenior/formtastic_with_mongoid_tutorial,但“TODO”在那里。

回答

5

我写了formtastic_with_mongoid_tutorial,不幸的是我还没有想出一个处理嵌入式关系的简单方法。我现在正在做的是在控制器中构建嵌入式对象,然后将对象传递到块中。它看起来有点像这样:

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

不要忘记在Team处理嵌套的属性:

class Team 
    include Mongoid::Document 
    accepts_nested_attributes_for :players, 
    :allow_destroy => true, 
    # formtastic sends blank attributes to Mongoid models if you use checkboxes 
    :reject_if => proc { |attributes| 
     attributes['name'].blank? && attributes['_destroy'].blank? 
    } 
    # ... 
end 

距离理想的绝对远。希望我能有更多的帮助,但也许这会让你指向正确的方向。我会密切关注更好的解决方案,并在发现任何问题时更新教程。