Ruby on rails 处理复杂表单的更好方法

Ruby on rails 处理复杂表单的更好方法,ruby-on-rails,database,Ruby On Rails,Database,这是一篇重新发布的文章——我删除了第一篇文章,因为我觉得我发布得很差 我是编程新手,我正在尝试处理一个ROR表单,它将数据发送到4个不同的模型/表,此时我的头撞到了墙上。总之,用例是教师在表单中输入以下内容:错误、相应的更正、错误的抽象、更正的抽象、描述抽象的一些标记以及解释 当我按submit时,屏幕上没有任何错误,但当我查看服务器时,唯一成功提交的是原始错误和更正-我得到一个未经允许的参数:其余的ec_抽象是嵌套的第一级。我开始觉得我只是把整个问题搞错了 表单看起来像这样,是否应该在同一个查

这是一篇重新发布的文章——我删除了第一篇文章,因为我觉得我发布得很差

我是编程新手,我正在尝试处理一个ROR表单,它将数据发送到4个不同的模型/表,此时我的头撞到了墙上。总之,用例是教师在表单中输入以下内容:错误、相应的更正、错误的抽象、更正的抽象、描述抽象的一些标记以及解释

当我按submit时,屏幕上没有任何错误,但当我查看服务器时,唯一成功提交的是原始错误和更正-我得到一个未经允许的参数:其余的ec_抽象是嵌套的第一级。我开始觉得我只是把整个问题搞错了

表单看起来像这样,是否应该在同一个查看页面上拆分为多个表单

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

任何帮助都将不胜感激。谢谢

您需要将log/development.log中的参数与CorrectionStoragesControllerec\u解释\u参数所允许的参数进行比较

错误

不允许的参数:ec_抽象

这意味着拒绝了参数ec_abstractions,因为它不在您的白名单中。要让表单提交一个名为ec_abstractions_attributes的参数,需要在电子解释模型中进行设置

当窗体具有嵌套属性时,如:

<%= form_for @ec_explanation do |ec_explanation_form| %>
  ...
  <%= ec_explanation_form.fields_for :ec_abstractions do |ec_abstractions_form| %>
    ...
那么你需要:

class EcExplanation < ApplicationRecord
  accepts_nested_attributes_for :ec_abstractions

问题是,我接受ec_抽象和ec_解释之间的连接模型的嵌套属性,但我这样做只是因为我已经尝试了另一种方法。。。或者我是这么想的。。。无论如何,谢谢!不确定这是否是询问的适当位置,但现在我在屏幕上看到两条错误消息:ec抽象ec对响应必须存在,ec抽象ec对ec抽象必须存在。为了解决这个问题,我应该学习什么?不需要告诉我答案,只需给我指出正确的方向:nvm——它必须与关系有关。我将首先找出一种方法将参数添加到零件中——我想我不必一次创建所有参数,尽管这需要去掉强参数。有趣。对我来说,解决这个问题的方法是通过在后面添加'optional:true'使一些属于关系成为可选的。我不确定这是否会导致今后的问题——我想我会发现的。