首页 > 解决方案 > 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

标签: ruby-on-railsrubyformsruby-on-rails-6

解决方案


在你强大的参数中说:

  params.require(:evaluation).permit(:project_id, :user_id, :score, :name)

但在您看来,您有:

<%= fields_for :evaluations do |form| %>

删除最后一个sevaluations它应该可以工作。


与您的隐藏字段相关,您缺少使用<%=. 尝试:

<%= form.hidden_field :project_id, :value => params[:project_id] %>
<%= form.hidden_field :user_id, :value => mem.user_id %>

推荐阅读