2017-01-26 1 views
0

zu handhaben (das ist ein Re-Post - ich habe den ersten Beitrag gelöscht, weil ich denke, dass ich wirklich schlecht gepostet habe).Bessere Art komplexe form_for

Ich bin neu in der Programmierung, und ich versuche, ein ROR-Formular zu behandeln, das Daten an 4 verschiedene Modelle/Tabellen sendet, und an diesem Punkt hämmere ich meinen Kopf in die Wand. Zusammengefasst ist der Anwendungsfall, dass ein Lehrer das Folgende in eine Form eingibt: einen Fehler, eine entsprechende Korrektur, eine Abstraktion des Fehlers, eine Abstraktion der Korrektur, einige Markierungen, die die Abstraktion beschreiben, und eine Erklärung.

Wenn ich Submit drücken, bekomme ich keine Fehler auf dem Bildschirm, aber wenn ich auf den Server schaue, ist das einzige, was erfolgreich eingereicht wird, der ursprüngliche Fehler und die Korrektur - ich bekomme eine unpermitted parameter: ec_abstractions für den Rest (ec_abstractions ist die erste Verschachtelungsebene. Ich fange an zu denken, dass ich die ganze Frage falsch herumarbeite.

Das Formular sieht wie folgt aus (vielleicht in mehrere Formen auf der gleichen Ansicht Seite geteilt werden sollte?)

<%= form_for @ec_explanation do |ec_explanation_form| %> 
    <% if @ec_explanation.errors.any? %> 
    <div id="error_explanation"> 
     <h2><%= pluralize(@ec_explanation.errors.count, "error") %> prohibited this error-correction pair from being saved:</h2> 

     <ul> 
     <% @ec_explanation.errors.full_messages.each do |message| %> 
     <li><%= message %></li> 
     <% end %> 
     </ul> 
    </div> 
    <% end %> 

    <%#= Insert here: all (or a single) q_response(s) that have not yet been added %> 

    <div class="field"> 
    <%= ec_explanation_form.label :early_explanation %> 
    <%= ec_explanation_form.text_area :early_explanation %> 
    </div> 

    <div class="field"> 
    <%= ec_explanation_form.label :full_explanation %> 
    <%= ec_explanation_form.text_area :full_explanation %> 
    </div> 
    <!--(this is just a test field for the "number_field" data type - I'm not convinced that we should input the stage like this)--> 
    <div class="field"> 
    <%#= ec_explanation_form.label :stage %> 
    <%#= ec_explanation_form.number_field :stage %> 
    </div> 

    <%= ec_explanation_form.fields_for :ec_abstractions do |ec_abstractions_form| %> 

     <% if @ec_abstraction.errors.any? %> 
      <div id="error_explanation"> 
       <h2><%= pluralize(@ec_abstraction.errors.count, "error") %> prohibited this error-correction pair from being saved:</h2> 

       <ul> 
       <% @ec_abstraction.errors.full_messages.each do |message| %> 
        <li><%= message %></li> 
       <% end %> 
       </ul> 
      </div> 
     <% end %> 

     <div class="field"> 
     <%= ec_abstractions_form.label :error_abstraction %> 
     <%= ec_abstractions_form.text_field :error_abstraction %> 
     </div> 

     <div class="field"> 
     <%= ec_abstractions_form.label :correction_abstraction %> 
     <%= ec_abstractions_form.text_field :correction_abstraction %> 
     </div> 

     <%= ec_abstractions_form.fields_for :ec_pairs do |ec_pairs_form| %> 

     <div class="field"> 
      <%= ec_pairs_form.label :error_phrase %> 
      <%= ec_pairs_form.text_field :error_phrase %> 
     </div> 

     <div class="field"> 
      <%= ec_pairs_form.label :correction_phrase %> 
      <%= ec_pairs_form.text_field :correction_phrase %> 
     </div> 

     <% end %> 

     <%= ec_abstractions_form.fields_for :tags do |tags_form| %> 

     <div class="field"> 
      <%= tags_form.label :tag, "Tag" %> 
      <%= tags_form.text_field :tag %> 
     </div> 

     <div class="field"> 
      <%= tags_form.label :tag, "Tag" %> 
      <%= tags_form.text_field :tag %> 
     </div> 

     <div class="field"> 
      <%= tags_form.label :tag, "Tag" %> 
      <%= tags_form.text_field :tag %> 
     </div> 

     <% end %> 
    <% end %> 

    (Include javascript (or bootstrap) that will generate extra tag fields onclick of a 'plus' button) 

    <div class="actions"> 
    <%= ec_explanation_form.submit 'Submit Correction' %> 
    </div> 
<% end %> 

Ich habe folgende Modelle:

class EcExplanation < ApplicationRecord 
     has_many :abstractions_explanations_joins 
     has_many :ec_abstractions, :through => :abstractions_explanations_joins 
     accepts_nested_attributes_for :abstractions_explanations_joins 
    end 

    class AbstractionsExplanationsJoin < ApplicationRecord 
     belongs_to :ec_explanation 
     belongs_to :ec_abstraction 
     accepts_nested_attributes_for :ec_abstraction 
    end 

    class EcAbstraction < ApplicationRecord 
     has_many :ec_pairs 
     has_many :tags_ec_abstractions_joins 
     has_many :tags, :through => :tags_ec_abstractions_joins 
     has_many :abstractions_explanations_joins 
     has_many :ec_explanations, :through => :abstractions_explanations_joins 
     accepts_nested_attributes_for :tags_ec_abstractions_joins 
     accepts_nested_attributes_for :ec_pairs 
    end 

    class EcPair < ApplicationRecord 
     belongs_to :response 
     belongs_to :ec_abstraction 
    end 

    class TagsEcAbstractionsJoin < ApplicationRecord 
     belongs_to :ec_abstraction 
     belongs_to :tag 
     accepts_nested_attributes_for :tag, :reject_if => lambda { |a| a[:tag].blank? } 
    end 

    class Tag < ApplicationRecord 
     has_many :tags_ec_abstractions_joins 
     has_many :ec_abstractions, :through => :tags_ec_abstractions_joins 
    end 

und die folgenden Controller-Code:

class CorrectionStoragesController < ApplicationController 

    def index 
    @ec_explanations = EcExplanation.all 
    end 

    def show 
    end 


    def new 
    @ec_explanation = EcExplanation.new 
    @ec_abstraction = @ec_explanation.ec_abstractions.build 
    @ec_pair = @ec_abstraction.ec_pairs.build 
    3.times do 
     @tag = @ec_abstraction.tags.build 
    end 
    end 

    def edit 
    end 

    def create 
    @ec_explanation = EcExplanation.create(ec_explanation_params) 

    respond_to do |format| 
     if @ec_explanation.save 
     format.html { redirect_to new_correction_storage_path, notice: 'Correction storage was successfully created.' } 
     format.json { render :show, status: :created, location: @ec_explanation } 
     else 
     format.html { render :new } 
     format.json { render json: @ec_explanation.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    def update 
    respond_to do |format| 
     if @ec_explanation.update(ec_explanation_params) 
     format.html { redirect_to new_correction_storage_path, notice: 'Correction was successfully updated.' } 
     format.json { render :show, status: :ok, location: new_correction_storage_path } 
     else 
     format.html { render :edit } 
     format.json { render json: @ec_explanation.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    def destroy 
    @ec_explanation.destroy 
    respond_to do |format| 
     format.html { redirect_to correction_storages_url, notice: 'Correction was successfully destroyed.' } 
     format.json { head :no_content } 
    end 
    end 



    private 

    # Never trust parameters from the scary internet, only allow the white list through. 
    def ec_explanation_params 
     params.require(:ec_explanation).permit(:early_explanation, :full_explanation, ec_abstractions_attributes: [:id, :error_abstraction, :correction_abstraction, ec_pairs_attributes: [:id, :error_phrase, :correction_phrase], tags_attributes: [:id, :tag]]) 
    end 
end 

Jede Hilfe würde sehr geschätzt werden. Danke

Antwort

1

Sie wollen die Parameter in log/development.log gegen was von CorrectionStoragesController#ec_explanation_params erlaubt vergleichen.

Der Fehler

unpermitted Parameter: ec_abstractions

bedeutet, dass Strong Parameters die Parameter ec_abstractions abgelehnt, weil das nicht in ihrem Adressbuch ist. Damit das Formular einen Parameter mit dem Namen ec_abstractions_attributes sendet, müssen Sie in Ihrem EcExplanation-Modell accepts_nested_attributes_for einrichten.

Wenn Ihre Form hat verschachtelte Attribute wie:

<%= form_for @ec_explanation do |ec_explanation_form| %> 
    ... 
    <%= ec_explanation_form.fields_for :ec_abstractions do |ec_abstractions_form| %> 
    ... 

Dann brauchen Sie:

class EcExplanation < ApplicationRecord 
    accepts_nested_attributes_for :ec_abstractions 
+0

Das Problem ist, dass ich verschachtelte Attribute für das Join-Modell zwischen ec_abstractions und ec_explanations war die Annahme, aber ich war nur weil ich es schon anders versucht hatte ... oder so dachte ich ... jedenfalls, danke! Nicht sicher, ob dies der richtige Ort ist, um zu fragen, aber jetzt bekomme ich 2 Fehlermeldungen auf dem Bildschirm: "ec abstractions ec pairs Antwort muss existieren", und "ec abstractions ec pairs ec Abstraktion muss existieren". Was soll ich lernen, um das zu beheben? (Keine Notwendigkeit, mir die Antwort zu sagen, zeigen Sie mir nur in die richtige Richtung :)) – michaelsking1993

+0

nvm - es muss mit Angle_to Beziehungen zu tun haben. Ich werde einen Weg finden, den Teil "gehört" zuerst hinzuzufügen - ich denke, ich muss nicht alle Parameter auf einmal erstellen, obwohl das die Beseitigung der starken Parameter beinhalten würde. Interessant. – michaelsking1993

+0

Was das Problem für mich gelöst hat, war, einige der 'gehört_to'-Beziehungen optional zu machen, indem ich 'optional: true' nachher hinzufüge. Ich bin mir nicht sicher, ob das Probleme auf der Straße verursachen wird - ich denke, ich werde es herausfinden. – michaelsking1993

Verwandte Themen