Ruby on rails 如何为多对多模型构建Rails?
我有一个用户、一个Wiki和一个协作者模型:Ruby on rails 如何为多对多模型构建Rails?,ruby-on-rails,form-for,Ruby On Rails,Form For,我有一个用户、一个Wiki和一个协作者模型: class User < ActiveRecord::Base has_many :wikis has_many :collaborators end class Wiki < ActiveRecord::Base has_many :wikis has_many :collaborators end class Collaborator < ActiveRecord::Base belongs_to :us
class User < ActiveRecord::Base
has_many :wikis
has_many :collaborators
end
class Wiki < ActiveRecord::Base
has_many :wikis
has_many :collaborators
end
class Collaborator < ActiveRecord::Base
belongs_to :user
belongs_to :wiki
end
class用户
当我编辑Wiki的合作者时,我希望表单如下所示:
我的问题是我不知道如何构造表单。我原以为下面的代码可以工作,但是
不会导致结果页面中包含任何内容
<% form_for :collaborator do |f|%>
<% possible_collaborators.each do |user| %>
<%= check_box_tag 'wiki[collaborator_ids][]', user.id, wiki.collaborators.include?(user) %>
<%= user.name %>
<br />
<% end %>
<%= f.submit %>
<% end %>
由于您没有发布控制器代码,我无法完全确定您想要完成什么,但我认为您错过了接受嵌套属性的功能(,)
此外,我只能向您推荐使用或,因为当涉及到复杂表单时,这些gem做得很好,并且在基本用例方面对您有很大帮助