I'm a newbie stumbling while creating my first rails app. I am trying to add several records to a table from a nested form, and at the moment only the last record is being added.
I am working on a form that will allow the user to associate a mathematical equation with how it should be read in accordance with this reading rule. In the abstract representation, there are two simple entries:
equation: "x-3", readingRule:"Simple", transcription"x take away three" equation: "x-3", readingRule:"Standard", transcription"x minus three"
I have four tables: "equations", "transcriptions", "readRuleSets" and "tests". One test consists of equation identifiers, transcription, and readRuleSet.
I have a form that has a text field for the user to select the equation identifier and four text fields (associated with my four sets of reading rules) so that they can select the transcription identifier. When I got to submit, I want to add four new βtestsβ, one for each transcription. Currently, Rails adds only the latter.
I thought this was because the identifier of the text fields is the same in the html source. I tried to associate the field name with the index from each_with_index, but that left me with one record added to 'test', and read_rule_set_id was empty because I would change the name of the column with the index. So I figured it out, read a lot more, looked at railscasts 196, and I'm still stuck.
Corresponding code bits:
\ App \ models \ test.rb
class Test < ActiveRecord::Base has_many :equations has_many :reading_rule_sets has_many :transcriptions accepts_nested_attributes_for :equations :transcriptions :reading_rule_sets end
The remaining three tables have their respective attributes belong to. '.
\ application \ Views \ tests:
<div> <fieldset> <legend> Reading Rules and Transcriptions </legend> <% ReadingRuleSet.all.each_with_index do |rrs, index| %> <div class="row"> <div class="col-md-6"> <label><%= rrs.name %></label> </div> <div class="col-md-6"> <%= f.text_field :transcription_id %> <%= f.hidden_field :reading_rule_set_id, :value =>rrs.id %> </div> </div> <% end %> </fieldset> </div> <div class="actions"> <%= f.submit %> </div>
Application \ Controllers \ tests_controller.rb