Jestem nowicjuszem, który natknął się na moją pierwszą aplikację railsową. Próbuję dodać wiele rekordów do tabeli z zagnieżdżonego formularza, a obecnie dodawany jest tylko ostatni rekord.Dodawanie wielu rekordów w Railsach
Pracuję nad formularzem, który pozwoli użytkownikowi powiązać równanie matematyczne z tym, jak powinien być czytany pod określoną zasadą odczytu. W abstrakcyjny widzenia, dwa proste zapisy byłoby:
equation: "x-3", readingRule:"Simple", transcription"x take away three"
equation: "x-3", readingRule:"Standard", transcription"x minus three"
Mam cztery tabele: „równania”, „”, „transkrypcje readingRuleSets” i „testy”. Pojedynczy test składa się z identyfikatorów równania, transkrypcji i odczytuRozdział.
Mam formularz, który ma pole tekstowe dla użytkownika, aby wybrać identyfikator równania i cztery pola tekstowe (powiązane z moimi czterema zestawami reguł czytania), aby umożliwić im wybranie identyfikatora transkrypcji. Kiedy wykonuję zgłoszenie, chcę dodać cztery nowe "testy", po jednym dla każdego z transkrypcji. W tej chwili Rails dodaje tylko ostatnią.
Myślałem, że to dlatego, że id w polach tekstowych są takie same w źródle html. Próbowałem połączyć nazwę pola z indeksem z each_with_index, ale to pozostawiło mnie z jednym rekordem dodawanym do 'test', a read_rule_set_id był pusty, ponieważ zmieniłem nazwę kolumny z indeksem. Więc zabrałem się za to, przeczytałem o wiele więcej, oglądałem railscasty 196, i nadal tkwię.
Istotne fragmenty kodu:
\ app \ modeli \ 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
Pozostałe trzy tabele mają swoje odpowiednie „belongs_to tych.
\ app \ views \ testy:
<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
# 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