简体   繁体   English

在Rails中添加多个记录

[英]Adding multiple records in Rails

I'm a newbie stumbling my way through creating my first rails application. 我是一个新手,通过创建我的第一个rails应用程序绊脚石。 I'm trying to add multiple records to a table from a nested form, and at the moment, only the last record is being added. 我正在尝试从嵌套表单向表中添加多个记录,目前只添加了最后一条记录。

I'm working on a form that will allow a user to associate a mathematics equation with how it should be read under a given reading rule. 我正在制作一个表格,允许用户将数学方程与在给定阅读规则下应该如何阅读相关联。 In an abstract view, two simple records would be: 在抽象视图中,两个简单记录将是:

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', 'readingRuleSets', and 'tests'. 我有四个表:'方程','转录','readingRuleSets'和'测试'。 A single test consists of the ids of an equation, a transcription, and a readingRuleSet. 单个测试由等式的id,转录和readingRuleSet组成。

I have a form, which has a text field for the user to select the id of the equation, and four text fields (associated with my four reading rule sets) for them to select the id of the transcriptions. 我有一个表单,其中有一个文本字段供用户选择方程式的id,以及四个文本字段(与我的四个阅读规则集相关联),供他们选择转录的id。 When I hit submit, I want four new 'tests' added, one for each of the transcriptions. 当我点击提交时,我想要添加四个新的“测试”,每个转录一个。 At the moment, Rails is adding only the last one. 目前,Rails只添加了最后一个。

I thought it was because the id's of the text fields are the same in the html source. 我认为这是因为html源中文本字段的id是相同的。 I tried concatenating the field name with the index from each_with_index, but that left me with one record being added to 'test', and the reading_rule_set_id was null because I'd amended the name of the column with the index. 我尝试将字段名称与each_with_index中的索引连接起来,但这使得我将一条记录添加到'test',并且reading_rule_set_id为null,因为我修改了带有索引的列的名称。 So I've taken that out, read a lot more, watched railscasts 196, and I'm still stuck. 所以我已经把它拿出来了,看了很多,看了看看有轨电视196,我还是被卡住了。

Relevant bits of code: 代码的相关部分:

\\app\\models\\test.rb \\程序\\型号\\ 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 three tables have their respective 'belongs_to's. 其他三个表分别有'belongs_to'。

\\app\\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>

app\\controllers\\tests_controller.rb 应用\\控制器\\ 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

Check your test_params. 检查你的test_params。

See Rails guide nested-form 请参阅Rails指南嵌套表单

For my information, it is possible for the nested of nested and 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 I think it is better to pass json data (made by js JSON.stringify) because of complexity(?), intuition(?) and above all, front-end framework (eg angularjs (ng-model)) 但我认为最好传递json数据(由js JSON.stringify制作)因为复杂性(?),直觉(?)以及最重要的前端框架(例如angularjs(ng-model))

ps (?) means it could be only for me. ps(?)意味着它只适合我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM