2010-12-22 3 views
1

embeds_many-embedded_in 관계에 대한 양식을 빠르게 만들 수 있습니까? 나는 다음과 같습니다Formtastic with Mongoid embedded_in relations

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 

그것은 확실히 훨씬 이상적에서입니다. 더 많은 도움이 될 수 있으면 좋겠지 만, 아마도 이것은 올바른 방향으로 당신을 가리킬 것입니다. 더 나은 솔루션을 찾으려 고 할 것이고 튜토리얼을 업데이트 할 것입니다.