2017-01-26 6 views
0

(これは再投稿です - 私は本当に不十分に投稿したと思うので、私は最初の投稿を削除しました)。複雑なフォームを処理する方が良い

私はプログラミングが初めてで、4つの異なるモデル/テーブルにデータを送信するRORフォームを処理しようとしています。この時点で私は壁に頭を抱えています。要するに、教師は、エラー、対応する訂正、エラーの抽象化、訂正の抽象化、抽象を記述するいくつかのタグ、および説明をフォームに入力するというユースケースです。

私がサブミットを押すと、画面にエラーは表示されませんが、サーバーを見ると、正常に送信されたのは元のエラーと修正です。残りの部分はunpermitted parameter: ec_abstractionsですec_abstractionsは入れ子の最初のレベルです)。私はちょうど全体の質問について誤ってやっていると思っています。

フォームは、私は次のモデルを持っているこの(?多分同じビューのページに複数のフォームに分割する必要があります)

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

次のようになります。

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 

そして、次のコントローラコード:

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 

ご協力いただければ幸いです。ありがとう

答えて

1

log/development.logのパラメータとCorrectionStoragesController#ec_explanation_paramsの許容値を比較したいと思うでしょう。

エラー

許可されていないパラメータ:ec_abstractions

はそれがあなたのホワイトリストにないためStrong Parametersがパラメータec_abstractionsを拒否したことを意味します。フォームにec_abstractions_attributesというパラメータを送信させるには、EcExplanationモデルでaccepts_nested_attributes_forを設定する必要があります。

フォームが入れ子になっている属性のように:

class EcExplanation < ApplicationRecord 
    accepts_nested_attributes_for :ec_abstractions 
+0

問題は、私はec_abstractionsとec_explanations間の参加モデルのネストされた属性を受け入れたということでしたが、私はでした。そして、

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

次のものが必要私はすでに他の方法を試していたので、それだけをしていた...または私は考えた...とにかく、ありがとう!これが適切な場所であるかどうかは不明ですが、今は画面に2つのエラーメッセージが表示されています:「ec抽象クラスec応答が存在しなければなりません」、「ec抽象クラスec抽象クラスが存在する必要があります」これを修正するにはどうすればわかるのですか? (私に答えを教える必要はない、ちょうど正しい方向に向ける:)) – michaelsking1993

+0

nvm - それはbelongs_to関係と関係している必要があります。まずbelongs_toの部分を追加する方法を見つけます - 私はすべてのパラメータを一度に作成しなくてはなりませんが、それは強力なパラメータを取り除くことになります。面白い。 – michaelsking1993

+0

私が問題を解決したのは、後に 'optional:true'を追加することによって 'belongs_to'関係のいくつかをオプションにすることでした。これが問題を引き起こすかどうかは分かりません。私は見つけ出すでしょう。 – michaelsking1993

関連する問題