I'm working on a Ruby on Rails application using Rails 3.2 and ruby 1.9.3. I'm using mongodb with mongoid and I would like to ask you if there is a possibility to implement a single "fields for" for a single object in the one-to-many relationship.
class House
include Mongoid::Document
embeds_many :rooms
accepts_nested_attributes_for :rooms
field :city, type: String
end
class Room
include Mongoid::Document
embedded_in :house
field :color, type: String
field :area, type: String
end
I would like to do a View where you can add more Rooms, while you are creating a House. For instance,
- form_for @house do |f|
= f.input :city
(show the list of rooms I am creating)
- f.fields_for :rooms, @house.rooms.build do |builder|
= builder.input :color
= builder.input :area
The behaviour should be like the next:
You can create multiple rooms in the same form and when you finish to add these rooms, submit the house form in order to save the home with the rooms.
Thanks in advance people