我正在寻找一种管理表单中多个嵌入对象的方法。
找到了一个由bowsersenior开发的一种解决方案
Formtastic with Mongoid embedded_in relations
但我无法对simple_form
做同样的事情formtastic:
= semantic_form_for @team do |form|
= @team.players.each do |player|
= form.inputs :for => [:players, player] do |player_form|
= player_form.input :name
最好的问候
样品
class Team
include Mongoid::Document
field :name, :type => String
embeds_many :players
end
class Player
include Mongoid::Document
embedded_in :team, :inverse_of => :players
field :name, :type => String
field :active, :type=> Boolean # checkboxes
end
答案 0 :(得分:3)
不确定这是否有效,但你可能想尝试这样的事情:
= simple_form_for @team do |form|
= f.input :name
= f.simple_fields_for @team.players do |player_form|
= player_form.input :name
请记住,在表单出现之前,您必须在团队中创建一个新玩家。 在您的控制器(控制器)中:
def new
@team = Team.new
8.times { @team.players.new } #for 8 players
end