I have the following association
class Document < ActiveRecord::Base has_many :sections has_many :items, :through => :sections end class Section < ActiveRecord::Base belongs_to :document has_many :items end class Item < ActiveRecord::Base belongs_to :section end
Here is the sample view for the 'new' action <% form_for(@document) do |f| %> <%= f.error_messages %> <p> <p> Header <p/><br /> <%= f.label :hdrcomment %> <%= f.text_field :hdrcomment %> </p> <% for section in @document.sections %> <% f.fields_for section, :index => section do |s| %> <%= section.seqnum %><br /> <% for item in section.items %> <% s.fields_for item, :index => item do |i| %> <%= i.text_area :comments %><br /> <% end %> <% end %> <% end %> <% end %> <p> <%= f.submit "Submit Document" %> </p> <% end %>
What is way to save the header, sections and items. Do I have to iterate the request hash. The below code doesn't work. Gives ActiveRecord::UnknownAttributeError inDocumentsController#create
def create @document = Document.new(params[:document]) @document.save! end
Request: {"commit"=>"Submit Document", "authenticity_token"=>"+uOqJn1DSjzK6r1iIkaIqZa9VrKpU2Yr/A6zP0vg0eM=", "document"=>{"section"=>{"#<Section:0xb74045f4>"=>{"item"=>{"#<Item:0xb73d2860>"=>{"comments"=>"asdf"}, "#<Item:0xb73d1af0>"=>{"comments"=>"sdf"}}}, "#<Section:0xb7403794>"=>{"item"=>{"#<FeedbackItem:0xb73ce6fc>"=>{"comments"=>"sdf"}}}, "#<Section:0xb740ea90>"=>{"item"=>{"#<Item:0xb73e0e74>"=>{"comments"=>"asd"}}}}, "hdrcomment"=>" asdf"}}
Thanks much.