Adding Multiple Entries to Rails

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 %> <!--# .to_s + index.to_s--> </div> </div> <% end %> </fieldset> </div> <div class="actions"> <%= f.submit %> </div> 

Application \ Controllers \ tests_controller.rb

 # POST /tests # POST /tests.json def create @test = Test.new(test_params) respond_to do |format| if @test.save format.html { redirect_to @test, notice: 'Test was successfully created.' } format.json { render :show, status: :created, location: @test } else format.html { render :new } format.json { render json: @test.errors, status: :unprocessable_entity } end end end # PATCH/PUT /tests/1 # PATCH/PUT /tests/1.json def update respond_to do |format| if @test.update(test_params) format.html { redirect_to @test, notice: 'Test was successfully updated.' } format.json { render :show, status: :ok, location: @test } else format.html { render :edit } format.json { render json: @test.errors, status: :unprocessable_entity } end end end private # Use callbacks to share common setup or constraints between actions. def set_test @test = Test.find(params[:id]) end # Never trust parameters from the scary internet, only allow the white list through. def test_params params.require(:test).permit(:equation_id, :reading_rule_set_id, :transcription_id, :transcription_transcription) end 
+6
source share
1 answer

Check your test_parameters.

See the Rails nested-form manual

According to my data, embedding of a nested and nested polymorphic model is possible.

 #example def class_info_params params.require(:class_info).permit(..... classes_attributes : [:id, :_destroy, , class_times_attributes:[:id, :day, :start_time, :_destroy]], my_subcategories_attributes: [:id,:_destroy, :subcategoriable_id, :subcategoriable_type, :subcategory_id]) end 

But I think it is better to pass json data (made by js JSON.stringify) due to complexity (?), Intuition (?) And, above all, the interface structure (e.g. angularjs (ng-model))

ps (?) means this can only be for me.

0
source

All Articles