繁体   English   中英

在Rails中添加多个记录

[英]Adding multiple records in Rails

我是一个新手,通过创建我的第一个rails应用程序绊脚石。 我正在尝试从嵌套表单向表中添加多个记录,目前只添加了最后一条记录。

我正在制作一个表格,允许用户将数学方程与在给定阅读规则下应该如何阅读相关联。 在抽象视图中,两个简单记录将是:

equation: "x-3", readingRule:"Simple", transcription"x take away three" 
equation: "x-3", readingRule:"Standard", transcription"x minus three"

我有四个表:'方程','转录','readingRuleSets'和'测试'。 单个测试由等式的id,转录和readingRuleSet组成。

我有一个表单,其中有一个文本字段供用户选择方程式的id,以及四个文本字段(与我的四个阅读规则集相关联),供他们选择转录的id。 当我点击提交时,我想要添加四个新的“测试”,每个转录一个。 目前,Rails只添加了最后一个。

我认为这是因为html源中文本字段的id是相同的。 我尝试将字段名称与each_with_index中的索引连接起来,但这使得我将一条记录添加到'test',并且reading_rule_set_id为null,因为我修改了带有索引的列的名称。 所以我已经把它拿出来了,看了很多,看了看看有轨电视196,我还是被卡住了。

代码的相关部分:

\\程序\\型号\\ 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

其他三个表分别有'belongs_to'。

\\程序\\意见\\测试:

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

应用\\控制器\\ 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

检查你的test_params。

请参阅Rails指南嵌套表单

对于我的信息,嵌套的嵌套和嵌套的多态模型是可能的。

#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

但我认为最好传递json数据(由js JSON.stringify制作)因为复杂性(?),直觉(?)以及最重要的前端框架(例如angularjs(ng-model))

ps(?)意味着它只适合我。

暂无
暂无

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

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