我有一个相互关联的技能数据库,作为彼此的先决条件。在技能索引中,我希望能够搜索其他技能并添加 1 个或更多作为先决条件。重要的是要注意,我只希望用户能够添加先决条件,而不是删除它们,因为这是通过上下投票系统处理的。我正在使用 JQuery Tokeninput 并且实际上所有这些都可以工作,除了一件事:我无法弄清楚如何只添加先决条件,而不是在提交时替换特定技能的所有先决条件。
型号:
class Skill < ActiveRecord::Base
attr_accessible :skill_relationship_attributes, :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
attr_reader :prereq_tokens
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
查询:
$('#skill_prereq_tokens').tokenInput('/skills.json',
{ theme:'facebook',
propertyToSearch:'title',
queryParam:'search',
preventDuplicates:'true'
});
查看:
<%= simple_form_for skill do |f| %>
<%= f.input :prereq_tokens %>
<%= f.submit %>
<% end %>