Ruby on rails Rails:嵌套属性参数内的数组列参数数目错误(给定0,应为1..2)

Ruby on rails Rails:嵌套属性参数内的数组列参数数目错误(给定0,应为1..2),ruby-on-rails,ruby,cocoon-gem,Ruby On Rails,Ruby,Cocoon Gem,我不知道这是否相关,但我用的是蚕茧宝石 我的主要储蓄来源是财产 我已将此属性的属性嵌套为property\u unit\u attributes params.require(:property).permit( ... property_units_attributes: [:id, :rent, :floor, :suite_number, :square_feet, :unit_type[], :ceiling_height, :condition, :amenities, :_dest

我不知道这是否相关,但我用的是蚕茧宝石

我的主要储蓄来源是财产

我已将此属性的属性嵌套为
property\u unit\u attributes

params.require(:property).permit(
 ...
property_units_attributes: [:id, :rent, :floor, :suite_number, :square_feet, :unit_type[], :ceiling_height, :condition, :amenities, :_destroy]
);
这里的问题是
unit\u type
是接受这些属性的嵌套表单上的复选框

我不会包含整个表单,因为除了数组之外,所有属性都正确保存。下面是我在
:unit\u type

<div class="field">
        <%= f.check_box :unit_type, { multiple: true, class: 'field__checkbox' }, 'condominium', nil %>
        <label>Condominium</label>
      </div>

      <div class="field">
        <%= f.check_box :unit_type, { multiple: true, class: 'field__checkbox' }, 'general_business', nil %>
        <label>General Business</label>
      </div>

      <div class="field">
        <%= f.check_box :unit_type, { multiple: true, class: 'field__checkbox' }, 'health_care', nil %>
        <label>Health Care</label>
      </div>

      <div class="field">
        <%= f.check_box :unit_type, { multiple: true, class: 'field__checkbox' }, 'hostpitality_or_hotel', nil %>
        <label>Hospitality / Hotel</label>
      </div>

公寓
一般业务
保健
招待/酒店
以及呈现的HTML的示例:

<div class="field">
   <input class="field__checkbox" type="checkbox" value="land" name="property[property_units_attributes][0][unit_type][]" id="property_property_units_attributes_0_unit_type_land">
   <label>Land</label>
      </div>

土地
当我提交表单时,所有属性都显示在请求中。我将仅讨论以下属性:

“物业单元属性”=>{“0”=>{“租金”=>“33.0”,“楼层”=>“33”,“套房号”=>“[过滤],“平方英尺”=>“33”,“单元类型”=>[“公寓”、“工业”、“办公室”],“天花板高度”=>“33.0”,“条件”=>“3”,“便利设施”=>“3”,“id”=>“10”},

如您所见,我选中了三个复选框,它们被放置在一个数组中:

“单元类型”=>[“公寓”、“工业”、“办公室”]

在提交时,我遇到了以下错误:
参数数量错误(给定0,预期为1..2)

当我在嵌套属性(见上文)下的
:unit_type
参数上添加
[]
时,就会发生这种情况

如果我删除它,表单将提交,但该列不会保存

我想重申,所有其他字段都正确保存。只有此数组列完全忽略它


如果您需要来自应用程序其他部分的更多信息或更多代码,请在注释中告知我。

要将标量值数组列为白名单,请使用空数组传递哈希键:

params.require(:property).permit(
 ...
  property_units_attributes: [
      :id, :rent, :floor, :suite_number, 
      :square_feet, :unit_type, :ceiling_height, 
      :condition, :amenities, :_destroy,
      unit_type: []
  ]
)
这种语法看起来有点像黑魔法,但实际上只是普通的旧ruby:

irb(main):001:0> [:foo, :bar, baz: 1]
=> [:foo, :bar, {:baz=>1}]
为什么
:unit_type[]
会导致“参数数量错误(给定0,应为1..2)”

如果您尝试
:unit\u type[0]
可以看到它调用符号
:unit\u type
上的括号访问器方法

irb(main):014:0> :unit_type[0]
=> "u"
irb(main):015:0> :unit_type.method("[]")
=> #<Method: Symbol#[](*)>

当然,这并不能解决这个问题,因为Rack已经将任何以括号结尾的参数扩展为数组和散列。

感谢您分享知识。我是ruby的新手(从事其他人做的项目),不可否认,这让我有些不知所措。但是,我通过执行您所做的操作修复了我的问题-添加
单元类型:[]
并将其放置在
销毁
之后。我发现如果我不把它放在最后,我会得到一个语法错误是的,在Ruby中,像
foo:'bar'
这样的关键字总是必须位于任何方法调用的参数列表的末尾<代码>[1,2,foo:'bar']或
bar(1,2,baz:3)
有效,但
[1,foo:'bar',2]
bar(baz:3,1,2)
会创建语法错误。
:"unit_type[]"