我在rails 3.1中。我有以下型号我该如何在rails中处理这种类型的多级表单
class Tool < ActiveRecord::Base
has_many :comments
end
class Comment < ActiveRecord::Base
belongs_to :tool
has_many :relationships
has_many :advantages, :through => :relationships, :source => :resource, :source_type => 'Advantage'
has_many :disadvantages, :through => :relationships, :source => :resource, :source_type => 'Disadvantage'
end
class Relationship < ActiveRecord::Base
belongs_to :comment
belongs_to :resource, :polymorphic => true
end
class Disadvantage < ActiveRecord::Base
has_many :relationships, :as => :resource
has_many :comments, :through => :relationships
end
class Advantage < ActiveRecord::Base
has_many :relationships, :as => :resource
has_many :comments, :through => :relationships
end
总之,A Tool
有许多comments
。 A Comment
inturn与Advantages
和Disadvantages
相关联。因此,在我的tool/show
页面中,我将列出所有评论。
但是,如果我必须添加注释到工具页面,将会有一个表单,其中有一个textarea的评论和两个multi select list boxes
的优点和缺点。
这里有一个问题,如果用户想从现有的adv/disadvantagev中选择,用户可以从列表框中选择,或者如果用户想要添加一个新的副/他可以键入并添加它,以便通过ajax调用保存并将新的副/副列表添加到列表框中。我该如何做到这一点?
我要提及nested_form是宝石 – maxenglander
好点!谢谢! – Tilo