简体   繁体   English

如何在 Rails 5.2 中允许哈希数组

[英]How to permit array of hashes in Rails 5.2

I am trying to create multiple "Absence"s by posting:我试图通过发布来创建多个“缺席”:

Parameters: {"absences"=>[{"user_id"=>1, "lesson_id"=>25,
"excused"=>true}, {"user_id"=>2, "lesson_id"=>25, "excused"=>true}]}

However, I am not able to whitelist this format in the controller.但是,我无法在控制器中将此格式列入白名单。 I attempted to follow the solution from " How to use strong parameters with an objects array in Rails ".我试图遵循“ 如何在 Rails 中对对象数组使用强参数”中的解决方案。

In my case:就我而言:

def absence_params
  params.permit(absences: [:user_id, :lesson_id, :excused])
end

I get我得到

ActiveModel::UnknownAttributeError (unknown attribute 'absences' for Absence.):

Then I tried:然后我尝试:

Parameters: {"absence"=>[{"user_id"=>1, "lesson_id"=>25,
"excused"=>true}, {"user_id"=>2, "lesson_id"=>25, "excused"=>true}]}

def absence_params
  params.permit(:absence, array: [:user_id, :lesson_id, :excused])
end

and got:并得到:

Unpermitted parameters: :absence, :format

---- Resolved ---- ----解决了----

  1. The gem 'cancancan' was not allowing me to create using an array. gem 'cancancan' 不允许我使用数组进行创建。
  2. If you have an issue permitting an array in the strong params, try如果您在允许强参数中存在数组时遇到问题,请尝试

params.require(:absences).map do |p|
  p.permit(:user_id, :lesson_id, :excused)
end

Your parameters permit code is correct:您的参数许可代码是正确的:

require "bundler/inline"

gemfile(ENV['INSTALL'] == '1') do
  source "https://rubygems.org"
  gem "actionpack", "6.0.2.2"
  gem "activesupport", "6.0.2.2"
end

require "active_support/core_ext"
require "action_controller/metal/strong_parameters"
require "minitest/autorun"


class BugTest < Minitest::Test
  def test_stuff

    params = ActionController::Parameters.new({
      "absences"=>[
        {"user_id"=>1, "unpermitted_param" => 123, "lesson_id"=>25, "excused"=>true},
        {"user_id"=>2, "lesson_id"=>25, "excused"=>true}
      ]
    })

    assert_equal(
      {
        "absences"=>[
          {"user_id"=>1, "lesson_id"=>25, "excused"=>true},
          {"user_id"=>2, "lesson_id"=>25, "excused"=>true}
        ]
      },
      params.permit(absences: [:user_id, :lesson_id, :excused]).to_h
    )
  end
end

The error comes from some other place, most likely you're trying to do something like Absence.create(absence_params) , which will only work for single records.错误来自其他地方,很可能您正在尝试执行诸如Absence.create(absence_params) ,它仅适用于单个记录。

To create an array at once you should adjust other relevant code accordingly, for example:要一次创建一个数组,您应该相应地调整其他相关代码,例如:

  1. Manually handle the array like:手动处理数组,如:

     @absenses = params["absences"].map do |raw_absense_params| Absense.create!(raw_absense_params.permit(:user_id, :lesson_id, :excused)) end
  2. Employ accepts_nested_attrubutes_for :absenses for the parent model if you have any (probably Lesson ).使用accepts_nested_attrubutes_for :absenses为父模型,如果你有任何(可能是Lesson )。 The code for this will be cleaner, as Rails will handle most things for you, like cases when not all instances can be saved because of validation, etc.这段代码会更简洁,因为 Rails 会为你处理大部分事情,比如由于验证等原因无法保存所有实例的情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM