2010-12-22 96 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 

距離理想的絕對遠。希望我能有更多的幫助,但也許這會讓你指向正確的方向。我會密切關注更好的解決方案,並在發現任何問題時更新教程。