Formtastic与Mongoid embedded_in的关系

mia*_*iah 1 ruby-on-rails formtastic mongoid

有没有快速的方法为embeds_many-embedded_in关系制作表单?我有以下内容:

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
Run Code Online (Sandbox Code Playgroud)

我想为团队创建一个表格,为玩家提供嵌入式编辑.在那里看到https://github.com/bowsersenior/formtastic_with_mongoid_tutorial但"TODO".

bow*_*ior 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
Run Code Online (Sandbox Code Playgroud)

不要忘记处理嵌套属性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
Run Code Online (Sandbox Code Playgroud)

这绝对不是理想的.希望我能得到更多帮助,但也许这会指出你正确的方向.我会密切关注更好的解决方案,并在我找到任何内容时更新教程.