(这是一个重新发布的帖子-我删除了第一个帖子,因为我认为我的帖子真的很糟糕)。
我是编程新手,我正在尝试处理一个将数据发送到4个不同模型/表的ROR表单,而在这一点上,我的头撞到了墙上。总而言之,用例是教师在表单中输入以下内容:错误,相应的更正,错误的抽象,更正的抽象,描述抽象的一些标签,以及解释。
当我按下submit时,屏幕上没有任何错误,但当我查看服务器时,唯一成功提交的是原始错误和更正-我得到了其余部分的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任何帮助都将不胜感激。谢谢
发布于 2017-01-26 11:32:54
您需要将log/development.log中的参数与CorrectionStoragesController#ec_explanation_params允许的参数进行比较。
错误
不允许的参数: ec_abstractions
表示Strong Parameters拒绝了参数ec_abstractions,因为该参数不在您的白名单中。要让表单提交一个名为ec_abstractions_attributes的参数,需要在EcExplanation模型中设置accepts_nested_attributes_for。
当窗体具有如下嵌套属性时:
<%= 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_abstractionshttps://stackoverflow.com/questions/41863997
复制相似问题