ruby-on-rails - Rails 6 未从表单中报告 hidden_values 以进行多对象构造
问题描述
我正在尝试创建一个表单,为从查询中提取的每个用户填充字段,然后一次提交所有对象。当整个表单提交时,我的代码当前是如何设置的,只有非隐藏字段被发送到控制器。
<%= form_tag evaluations_path(method: :post) do |f| %>
<% Ingroup.where(group_id: Group.where(course_id: Project
.find(params[:project_id]).course_id)).each do |mem| %>
<h3>Evaluation for <%= "#{mem.user.Fname} #{mem.user.Lname}" %></h3>
<%= fields_for :evaluation do |form| %>
<% form.hidden_field :project_id, :value => params[:project_id] %>
<% form.hidden_field :user_id, :value => mem.user_id %>
<div class="field">
<%= form.label :score %>
<%= form.number_field :score %>
</div>
<div class="field">
<%= form.label :comment %>
<%= form.text_area :comment %>
</div>
<% end %>
<% end %>
<div class="actions">
<%= submit_tag "Submit" %>
</div>
<% end %>
编辑:我从终端和控制器视图中添加了一些额外的代码。编辑 2:从评估中删除了复数,但仍然没有看到正在传递的隐藏字段值。
终端错误:
Started POST "/evaluations?method=post" for 127.0.0.1 at 2020-12-02 21:55:08 -0500
Processing by EvaluationsController#create as HTML
Parameters: {"authenticity_token"=>"9pSgJJh3iE1doRy81Jhh3gHEgEJZt7pzcZw3C5EZeMEBh22VG8pmMKtHTwFml+Sj/XZmb3pBv6CmOLb9WvEVkQ==", "evaluation"=>{"score"=>"1", "name"=>"asdf"}, "commit"=>"Submit", "method"=>"post"}
(0.1ms) begin transaction
↳ app/controllers/evaluations_controller.rb:31:in `create'
Evaluation Exists? (0.2ms) SELECT 1 AS one FROM "evaluations" WHERE "evaluations"."user_id" IS NULL AND "evaluations"."project_id" IS NULL LIMIT ? [["LIMIT", 1]]
↳ app/controllers/evaluations_controller.rb:31:in `create'
(0.1ms) rollback transaction
↳ app/controllers/evaluations_controller.rb:31:in `create'
No template found for EvaluationsController#create, rendering head :no_content
Completed 204 No Content in 9ms (ActiveRecord: 0.3ms | Allocations: 5851)
控制器:
class EvaluationsController < ApplicationController
before_action :set_evaluation, only: [:show, :edit, :update, :destroy]
def new
@evaluation = Evaluation.new
end
def create
@evaluation = Evaluation.new(evaluation_params)
respond_to do |format|
if @evaluation.save
format.html { redirect_to @evaluation, notice: 'Evaluation was successfully created.' }
format.json { render :show, status: :created, location: @evaluation }
else
format.html { render :new }
format.json { render json: @evaluation.errors, status: :unprocessable_entity }
end
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_evaluation
@evaluation = Evaluation.find(params[:id])
end
# Only allow a list of trusted parameters through.
def evaluation_params
params.require(:evaluation).permit(:project_id, :user_id, :score, :name)
end
end
解决方案
在你强大的参数中说:
params.require(:evaluation).permit(:project_id, :user_id, :score, :name)
但在您看来,您有:
<%= fields_for :evaluations do |form| %>
删除最后一个s
,evaluations
它应该可以工作。
与您的隐藏字段相关,您缺少使用<%=
. 尝试:
<%= form.hidden_field :project_id, :value => params[:project_id] %>
<%= form.hidden_field :user_id, :value => mem.user_id %>
推荐阅读
- ios - 如何使用 Objective-C 将一对多关系数据保存到核心数据中
- c# - 如何在c#中获取层次结构数据
- sql - 显示购买了超过 1 种产品但来自同一订阅的客户
- r - 如何在 R 中绘制带有条件 p 值和置信区间的 ANOVA?
- java - 请解释 read([] bytes) 的微妙之处
- google-sheets - Google表格公式:如果子字符串在单元格范围内,则求和
- continuous-integration - 如何配置 CI 以在每个合并请求之前增加测试覆盖率的百分比?
- java - 在Java中将jks转换为p12
- knex.js - 如何针对远程数据库运行 Knex 迁移?
- excel - 使用链接到命令按钮的“另存为”功能将当前图形保存在用户窗体图表中?