在Ryan Bates 的 Railscast的帮助下,我正在尝试使用 Tokeninput 设置一个搜索即输入的表单。不幸的是,我无法通过自引用关联来解决这个问题。
在我的模型中:
class Skill < ActiveRecord::Base
attr_accessible :skill_relationship_attributes, :prereq_tokens
attr_reader :prereq_tokens
has_many :skill_relationships
has_many :prereqs, :through => :skill_relationships
has_many :inverse_skill_relationships, :class_name => 'SkillRelationship', :foreign_key => "prereq_id"
has_many :inverse_prereqs, :through => :inverse_skill_relationships, :source => :skill
accepts_nested_attributes_for :skill_relationships, :allow_destroy => true
def prereq_tokens=(ids)
self.prereq_ids = ids.split(",")
end
end
class SkillRelationship < ActiveRecord::Base
attr_accessible :skill_id, :prereq_id, :skill_attributes, :prereq_attributes
belongs_to :skill
belongs_to :prereq, :class_name => 'Skill'
end
我无力地整理了一个表格:
<%= form_for skill do |f| %>
<%= f.label :prereq_tokens, "Prerequisites" %><br/>
<%= f.text_field :prereq_tokens, data: {load: Skill.all} %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
我确实得到了一个样式精美的文本字段,但它在搜索时找不到任何东西,而且我不确定从这里去哪里。有任何想法吗?