2
votes

i m looking for a way to manage multiple embedded objects in a form.

found a solution for formtastic by bowsersenior

Formtastic with Mongoid embedded_in relations

but i wasnt able to do the same for 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

best regards

sample

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
1

1 Answers

3
votes

Not sure if this would work but you might want to try something like this:

= simple_form_for @team do |form|
  = f.input :name
  = f.simple_fields_for @team.players do |player_form|
    = player_form.input :name

Just keep in mind that you will have to create a new player in the team before the form will show up. In your controller(controller):

def new
  @team = Team.new
  8.times { @team.players.new } #for 8 players
end