Ruby on rails 4 Rails 4允许使用动态键嵌套散列

Ruby on rails 4 Rails 4允许使用动态键嵌套散列,ruby-on-rails-4,Ruby On Rails 4,在允许参数中包含动态键的嵌套哈希时面临问题。我已经提到了关于堆栈溢出的其他类似问题,但到目前为止运气不好。非常感谢您的帮助。下面是引发的错误 ActionController::UnpermittedParameters找到了未经许可的参数:志愿者\u插槽\u属性、注册\u插槽\u属性 {"event"=>{ "name"=>"ss", "volunteers_slots_attributes"=>{ "0"=>{"_destroy"=>"false", "nee

在允许参数中包含动态键的嵌套哈希时面临问题。我已经提到了关于堆栈溢出的其他类似问题,但到目前为止运气不好。非常感谢您的帮助。下面是引发的错误

ActionController::UnpermittedParameters找到了未经许可的参数:志愿者\u插槽\u属性、注册\u插槽\u属性

{"event"=>{
"name"=>"ss", 
"volunteers_slots_attributes"=>{
"0"=>{"_destroy"=>"false", "needed_count"=>""},
"1"=>{"_destroy"=>"false", "needed_count"=>""},
... 
}, 
"sign_up_slots_attributes"=>{
"0"=>{"_destroy"=>"false", "title"=>"", "quantity"=>""}, 
"1"=>{"_destroy"=>"false", "title"=>"", "quantity"=>""}, 
"2"=>{"_destroy"=>"false", "title"=>"", "quantity"=>""},
 .... 
}, 
"supplies_note"=>""}}
控制器:

 def event_params
    params[:event].permit(:name,:supplies_note,
    :volunteers_slots_attributes,
    :sign_up_slots_attributes)
 end
事件模型:

  accepts_nested_attributes_for :sign_up_slots, allow_destroy: true, reject_if: :all_blank
  accepts_nested_attributes_for :volunteers_slots, allow_destroy: true, reject_if: :all_blank

您需要确保
event.rb
模型文件具有接受嵌套属性

在您的情况下,它应该包含以下行:

accepts_nested_attributes_for :volunteers_slots, reject_if: :all_blank, allow_destroy: true
accepts_nested_attributes_for :sign_up_slots, reject_if: :all_blank, allow_destroy: true
# ...

您需要确保
event.rb
模型文件具有接受嵌套属性

在您的情况下,它应该包含以下行:

accepts_nested_attributes_for :volunteers_slots, reject_if: :all_blank, allow_destroy: true
accepts_nested_attributes_for :sign_up_slots, reject_if: :all_blank, allow_destroy: true
# ...

您可以始终使用类似于
params[:event]的内容。允许(:name:志愿者\u slot\u属性:['0','1'])
来允许嵌套属性。

您可以始终使用类似于
params[:event]的内容。允许(:name:志愿者\u slot\u属性:['0','1'])
以允许嵌套属性。

您必须更改强参数,如


参数require(:事件).permit(
:姓名,
:供应(注),
志愿者\u插槽\u属性:[
:_销毁,
:需要计数
],
注册\u插槽\u属性:[
:_销毁,
:标题,
:数量
]
)

您必须更改强参数,如


参数require(:事件).permit(
:姓名,
:供应(注),
志愿者\u插槽\u属性:[
:_销毁,
:需要计数
],
注册\u插槽\u属性:[
:_销毁,
:标题,
:数量
]
)

是,它包含的属性为。。我更新了问题是,它包含接受嵌套属性。。我更新了question@BemieChiu我无法硬编码['0','1',],因为它是动态的。@Bemie Chiu我无法硬编码['0','1',],因为它是动态的。是的,我尝试了。。。/events“0”处的ArgumentError不是有效的Repeat错误。错误没有意义,是否可以发送堆栈跟踪?感谢您的评论。。。我发现问题出在带有枚举字段的事件模型中。(迁移到rails 4)。你的解决方案有效!是的,我试过。。。/events“0”处的ArgumentError不是有效的Repeat错误。错误没有意义,是否可以发送堆栈跟踪?感谢您的评论。。。我发现问题出在带有枚举字段的事件模型中。(迁移到rails 4)。你的解决方案有效!