我遇到的问题是Formtastic没有显示具有has_many多态关系的相关表格中的信息。
有四种模式可供使用:NPC,Player,Ability和Xref_Ability。但我现在只关注两个:Player和Xref_Ability
我违反了交叉引用表的命名约定,因为xref_ability与NPC和玩家联系在一起。请参阅下面的Xref_Ability和Player的模型关系:
播放器
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和玩家的异能交叉参考表)
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
我添加了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>
你可以看到我尝试了收集,并返回了所有记录的下拉菜单...不是我想要的。我也尝试用.find_each代替.all和条件,这给了我一个no block(yield)错误。
我知道它与多态关系有关,以及Xref_ability如何不是Player独有的。它也与Player没有直接关系。如果我知道如何做到这一点,那么也许我可以解决这个问题。我在gitHub上找不到任何内容,所以建议表示赞赏。
答案 0 :(得分: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>