2013-10-21 2 views
0

Formtastic에서 has_many 다형성 관계가있는 관련 테이블의 정보를 표시하지 않는 문제가 있습니다.레일 Formtastic Polymorphic Relationships

NPC, Player, Ability 및 Xref_Ability의 네 가지 모델이 있습니다. 그러나 플레이어와 Xref_Ability

xref_ability가 NPC와 Players에 연결되어 있기 때문에 상호 참조 테이블의 명명 규칙을 위반했습니다.

플레이어 내가 추가 한

class XrefAbility < ActiveRecord::Base 
    attr_accessible :ability_id, :characterable_id, :characterable_type 

    #Validation 
    validates :ability_id, :uniqueness => {:scope => [:characterable_id, :characterable_type]} 
    #Relations 
    belongs_to :ability 
    belongs_to :characterable, :polymorphic => true 
end 

class Player < ActiveRecord::Base 
    attr_accessor :characterable 
    ... 
    has_many :xref_abilities, :as => :characterable 
    #has_many :xref_abilities, :through => :characterable 
    accepts_nested_attributes_for :xref_abilities 
end 

Xref_Ability (모두 NPC들과 선수에 대한 능력에 대한 상호 참조 테이블) : Xref_Ability 아래 플레이어의 모델 관계를 참조하십시오 accepts_nested_attributes_for 많은 사람들이 이것없이 문제를 경험 한 것처럼 보였습니다. 그러나, 일단 그것을 포함하면 루프에 대한 입력에 :xref_abilities을 사용할 수 없습니다. 오류 : uninitialized constant Player::Xrefability. 그래서 내가 사용하려고 : characterable를하고는 null를 돌려 : 당신은 내가 수집을 시도하고 그것이 내가 찾던하지 무엇을 모든 기록 ...와 드롭 다운을 돌려 볼 수 있습니다

<div class="show"> 
    <%= semantic_form_for @Player do |f| %> 
    <h1><%= @Player .name %></h1> 
    <%= f.inputs %> 
    <%#= f.input :characterable, :collection => XrefAbility.all %> 
    <%= f.inputs :for => :characterables do |abil| %> 
     <%= abil.input :ability_id %> 
    <% end %> 
    <% end %> 
</div> 

. 나는 또한 .all 대신에 .find_each를 시도해 보았다. 조건과 함께, 블럭 (yield) 에러가 발생하지 않았다.

다형성 관계와 Xref_ability가 Player에만없는 이유가 있습니다. 또한 플레이어와 직접적인 관계가 없습니다. 이 작업을 수행하는 방법을 알고 있다면 아마도이 문제를 해결할 수있을 것입니다. gitHub에서 아무 것도 찾을 수 없으므로 제안 사항에 감사드립니다.

답변

0

이 수정 된 내용으로 시도해보십시오.

<div class="show"> 
    <%= semantic_form_for @Player do |f| %> 
    <h1><%= @Player .name %></h1> 
    <%#= f.inputs %> 
    <%#= f.inputs :characterable, :collection => XrefAbility.all %> 
    <%= f.semantic_fields_for :characterables do |abil| %> 
     <%= abil.inputs :ability_id %> 
    <% end %> 
    <% end %> 
</div> 
+0

semantic_fields_for 및 : for는 서로 바꿔서 사용할 수 있습니다. 같은 결과를 보였습니다 : null. – Andrew