mysql - Adding multiple records in Rails -
i'm newbie stumbling way through creating first rails application. i'm trying add multiple records table nested form, , @ moment, last record being added.
i'm working on form allow user associate mathematics equation how should read under given reading rule. in abstract view, 2 simple records be:
equation: "x-3", readingrule:"simple", transcription"x take away three" equation: "x-3", readingrule:"standard", transcription"x minus three" i have 4 tables: 'equations', 'transcriptions', 'readingrulesets', , 'tests'. single test consists of ids of equation, transcription, , readingruleset.
i have form, has text field user select id of equation, , 4 text fields (associated 4 reading rule sets) them select id of transcriptions. when hit submit, want 4 new 'tests' added, 1 each of transcriptions. @ moment, rails adding last one.
i thought because id's of text fields same in html source. tried concatenating field name index each_with_index, left me 1 record being added 'test', , reading_rule_set_id null because i'd amended name of column index. i've taken out, read lot more, watched railscasts 196, , i'm still stuck.
relevant bits of code:
\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 other 3 tables have respective 'belongs_to's.
\app\views\tests:
<div> <fieldset> <legend> reading rules , transcriptions </legend> <% readingruleset.all.each_with_index |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> app\controllers\tests_controller.rb
# post /tests # post /tests.json def create @test = test.new(test_params) respond_to |format| if @test.save format.html { redirect_to @test, notice: 'test 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 |format| if @test.update(test_params) format.html { redirect_to @test, notice: 'test 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 share common setup or constraints between actions. def set_test @test = test.find(params[:id]) end # never trust parameters scary internet, allow white list through. def test_params params.require(:test).permit(:equation_id, :reading_rule_set_id, :transcription_id, :transcription_transcription) end
check test_params.
see rails guide nested-form
for information, possible nested of nested , nested polymorphic model.
#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 think better pass json data (made js json.stringify) because of complexity(?), intuition(?) , above all, front-end framework (e.g. angularjs (ng-model))
p.s. (?) means me.
Comments
Post a Comment