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